babelcodes
9/16/2017 - 6:18 PM

TypeScript > Tuple

TypeScript > Tuple ▶︎ babel.codes/TypeScript/Tuple

// Tuple (= array with known size and types can be differents)
let x: [string, number];
x = ["hello", 10];
x = [10, "hello"]; // Error
console.log(x[0].substr(1));
console.log(x[1].substr(1)); // Error
// When accessing an element outside, a union type is used instead:
x[3] = "world"; // is 'string | number'
console.log(x[5].toString()); // string and number has toString
x[6] = true; // Error, 'boolean' isn't 'string | number'