TypeScript, by its nature, will try to infer types as specifically as possible without getting in our way.
It will always assume a list to be an array.
We need to explicitly state the type of a tuple whenever we declare one.
let myCar = [2002, 'Toyota', 'Corolla'];
// let myCar: (string | number)[]
To make it a tuple:
let myCar: [number, string, string] = [2002, 'Toyota', 'Corolla'];
// let myCar: [number, string, string]
Limitations
As of
TypeScript 4.3
there's limited support for enforcing tuple length constraints.
For example, you get the support you'd hope for on assignment:
const numPair: [number, number] = [4, 5, 6]; // <- ERROR
Error msg: Type '[number, number, number]' is not assignable to type '[number, number]'. Source has 3 element(s) but target allows only 2.
But you don't get support around push
and pop
:
const numPair: [number, number] = [4, 5];
numPair.push(6); // [4, 5, 6]
numPair.pop(); // [4, 5]
numPair.pop(); // [4]
numPair.pop(); // []
From the TypeScript Fundamentals, v3↗ course on FEM↗ taught by Mike North↗.