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:


This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.