Tuple Type in TypeScript

Last updated on 10 Jan, 2023

Vanilla JavaScript does not contain tuple data type. TypeScript brings Tuple Type.

What is Tuple?

It is an array with fixed length and fixed value. Example:

const role = [4, "teacher"];

Above array elements have a meaning. 4 is role id and "teacher" is role description. Any other array values like, [4, "teacher", true] is invalid.

We cannot create a tuple in vanilla JavaScript.

Defining a Tuple in TypeScript

Here is how we can set a tuple type using TypeScript.

let role: [number, string];

Above type says, role can have an array whose length should be 2. Also, the first element should be a number and second one a string.

--- ○ ---
Joby Joseph
Web Architect