What Is a Tuple in TypeScript?

In TypeScript, a tuple is simply a typed array in which types for each index are specified/known. For example:

type tuple = [string, number, string];

The tuple type in this example specifies that an array should contain exactly three elements, with their expected types specified at corresponding indexes. This means that the following is valid:

const ex: tuple = ['foo', 1, 'bar'];

And, the following would be invalid:

// Error: Type '[string]' is not assignable to type 'tuple'.
// Source has 1 element(s) but target requires 3.
const ex: tuple = ['foo'];

With tuples, you can also do the following:


Hope you found this post useful. It was published . Please show your love and support by sharing this post.