Tuples
Typed arrays.
Define Tuple
Section titled “Define Tuple”let aTuple: [number, boolean, string];
// initialize correctlyaTuple = [3, true, 'Hello World!'];
// initialize incorrectly; throws an error;// order of types must matchaTuple = [true, 'Hello Wold!', 3];
Readonly Tuple
Section titled “Readonly Tuple”// We have no type safety in our previous tuple for// array indices 3+aTuple.push('added after initialization');console.log(aTuple); // works because it isn't readonly
// define readonly tupleconst aReadonlyTuple: readonly [number, boolean, string] = [ 3, true, 'Hello World!',];
aReadonlyTuple.push('added after initialization');// throws error as it is readonly.
Named Tuples
Section titled “Named Tuples”Named tuples give us better context.
const graph: [x: number, y: number] = [51.0, 49.0];
Destructuring Tuples
Section titled “Destructuring Tuples”Tuples are arrays and they can be destructured as such.
const graph2: [number, number] = [51.0, 49.0];const [x, y] = graph;