Ad space (banner)
Lesson 16 / 20

Tuple Types

This lesson covers tuple types โ€” an array with a fixed number of elements and a fixed type for each one. It's for anyone searching "TypeScript tuple usage."

A tuple type is written as [Type1, Type2, ...], letting you specify a different type at each position of the array. Unlike a regular array, both the number of elements and their order are fixed as part of the type.

The example defines a tuple type, type NameAge = [string, number];, treating a name and an age as one bundled pair, then unpacks each element into its own variable using destructuring.

A common early mistake is not seeing the difference from a regular array (like string[]). A regular array can hold any number of elements, while a tuple type is different in that each position has a fixed meaning โ€” "first is a string, second is a number."

In real projects, tuple types are used when a function returns multiple values bundled together, or to represent a "fixed-order pair" like a coordinate (x, y).

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run TypeScript directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)