July 2018
Intermediate to advanced
420 pages
8h 46m
English
A tuple is like an organized typed array. Let's create one to see how it works:
const organizedArray: [number, string, boolean] = [0, 'text', false];let myArray: [number, string, boolean];myArray = ['text', 0, false]console.log(myArray);
The preceding code looks fine for JavaScript, but in TypeScript, we must respect the variable type; here, we are trying to pass a string where we must pass a number.
tsc tuple.ts
You will see the following error message:
tuple.ts(4,1): error TS2322: Type '[string, number, false]' is not assignable to type '[number, string, boolean]'. Type 'string' is ...
Read now
Unlock full access