Issue
Typescript in tuple allows to add extra elements with any of types used before, but I would like to limit the length. I've tried with & { length: 2 }, but it didn't helped:
declare var a: [string, number] & { length: 2 };
a[0] = "1"
a[1] = 2;
a[2] = "3";
a[3] = 4;
Assignments for [2] and [3] don't produce error. How can I specify corresponding type?
Solution
Use type never for array tail:
declare var a: [string, number, ...never[]];
and you'll get
Type '"3"' is not assignable to type 'never'.
Type '4' is not assignable to type 'never'.
Answered By - Qwertiy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.