Answer the question
In order to leave comments, you need to log in
Why is it allowed to add a value to a tuple?
There is this code:
let qwe: [string, number];
qwe = ['qqq', 4]
qwe.push(7);
qwe[1] = 44
qwe[2] = 77
console.log(qwe)
qwe[2] = 77
Answer the question
In order to leave comments, you need to log in
Because a tuple in ts is a subtype of an array, and the type system of ts is so poor that it cannot be expressed through it that some methods of the main type are not available on the subtype.
qwe[2] = 77
There are just 2 type errors at once: [string, number]
- this is quite a sugar over the type{readonly length: 2; 0: string; 1: number} & Readonly<typeof Array.prototype>
'length' | 0 | 1 | keyof typeof Array.prototype
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question