
Tuples
So far we’ve looked at arrays with all elements of the same type. But, as we know, JavaScript arrays are flexible and can have elements of different types. With TypeScript, we can also define arrays with a fixed sequence of types:
let ourTuple: [string, number, string, boolean] = ['Is', 7, 'our favorite number?', false]
In TypeScript, when an array is typed with elements of specific types, it’s called a tuple. The tuple above (ourTuple) contains the elements: 'Is', 7 , 'our favorite number?' , false and the tuple has a type of [string, number, string, boolean]. Tuple types specify both the lengths and the orders of compatible tuples, and will cause an error if either of these conditions are not met:
let numbersTuple: [number, number, number] = [1, 2, 3, 4]
// Type Error! numbersTuple should only have three elements.
let mixedTuple: [number, string, boolean] = ['hi', 3, true]
// Type Error! The first elements should be a number,
//the second a string, and the third a boolean.
As far as JavaScript is concerned, tuples act just like arrays. They both have .length properties. We can access (or change) the elements of both using [index]. But despite their similarities, tuples and arrays do not have compatible types within TypeScript. Specifically, we can’t assign an array to a tuple variable, even when the elements are of the correct type:
let tup: [string, string] = ['hi', 'bye']
let arr: string[] = ['there', 'there']
tup = ['there', 'there'] // No Errors.
tup = arr
// Type Error! An array cannot be assigned to a tuple.
Related articles
10 Essential Steps for Migrating Your JavaScript Project to TypeScript in 2025
Discover the practical roadmap to seamlessly transition your JavaScript projects to TypeScript, enhancing type safety and developer productivity in 2025.
Crafting Dynamic Data-Driven User Interfaces with React and TypeScript
Learn how to effortlessly craft dynamic, data-driven user interfaces using React and TypeScript, enhancing your app's responsiveness and user experience.
Mastering Next-Gen API Security: JWTs with Node.js & TypeScript in 2025
Learn step-by-step how to enhance your API’s security using JWTs with Node.js and TypeScript, preparing your applications for the security demands of 2025.