Search⌘ K

Tuple For Type and Length Arrays

Explore how to declare and use tuples in TypeScript, focusing on fixed-length and typed arrays. Understand how to enforce tuple length, utilize readonly tuples, and see how tuples transpile to JavaScript arrays for practical type-safe coding.

Tuple syntax #

The tuple type is an array of defined elements. To declare a tuple, you use square brackets, as in line 1, but instead of specifying a value, you use a type.

TypeScript 3.3.4
let numberTuple: [number, number, number];

A tuple can handle many different types. The tuple uses the same syntax as other types.

TypeScript 3.3.4
let myTupleWithTwoTypes: [number, string];

There is no difference between the declaration of a variable and the assignment. TypeScript checks for type ...