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?
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'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With