Back to questionsWhich type does TypeScript infer for
JuniorTheoryCommon
Which type does TypeScript infer for pair?
const pair = ["Аня", 30];(string | number)[]
- Expecting the tuple
[string, number]: an array literal may change later, so TypeScript infers an array, and a tuple is set with an annotation. - Expecting the type of the first element alone,
string[]: TypeScript joins the types of all the elements intostring | number.
- How would you declare
pairso thatpair[0]has the typestring? - Why does TypeScript not infer a tuple from an array literal?