Skip to content
DeveloperMemos

Using TypeScript Tuple Types

TypeScript, Programming, Development2 min read

Tuple types are a powerful feature in TypeScript that allow you to represent fixed-length arrays where each element can have a different data type. They provide a convenient way to define and work with structured data, such as coordinates, key-value pairs, or any other collections of elements with a known order.

Defining Tuple Types

In TypeScript, you can define tuple types by specifying the types of each element in square brackets [], separated by commas. For example, let's define a tuple type representing a 2D point:

1type Point2D = [number, number];

In this case, Point2D is a tuple type that consists of two elements, both of which are numbers. You can also assign values to variables of this type:

1const point: Point2D = [3, 7];

Accessing Tuple Elements

To access individual elements of a tuple, you can use array-like indexing and provide the element's index. TypeScript uses zero-based indexing, so the first element has an index of 0, the second element has an index of 1, and so on. For example:

1const x: number = point[0];
2const y: number = point[1];
3console.log(`x: ${x}, y: ${y}`); // Output: x: 3, y: 7

You can also use array destructuring to assign tuple elements to separate variables in a more concise way:

1const [x, y] = point;
2console.log(`x: ${x}, y: ${y}`); // Output: x: 3, y: 7

Modifying Tuple Elements

Tuple elements are mutable by default. You can modify individual elements by assigning new values to them. However, the length and the types of the elements in a tuple cannot be changed after it is defined. For example:

1point[0] = 5;
2console.log(`Modified x: ${point[0]}`); // Output: Modified x: 5

Using Tuple Types in Functions

Tuple types are not limited to variable declarations; they can also be used as function parameter and return types. This allows you to enforce the expected structure and types of arguments and return values.

1function calculateDistance(point1: Point2D, point2: Point2D): number {
2 const dx = point2[0] - point1[0];
3 const dy = point2[1] - point1[1];
4 return Math.sqrt(dx ** 2 + dy ** 2);
5}
6
7const distance = calculateDistance([0, 0], [3, 4]);
8console.log(`Distance: ${distance}`); // Output: Distance: 5

In the example above, the calculateDistance function takes two Point2D tuples as arguments and returns a number. By using tuple types, we ensure that the function receives the correct number of arguments of the expected types.

Rest Elements in Tuple Types

TypeScript also supports rest elements in tuple types, denoted by the ... syntax. A rest element allows you to represent a variable number of elements of the same type. Any additional elements beyond the explicitly defined ones are collected into an array.

1type StringNumberTuple = [string, ...number[]];
2
3const tuple1: StringNumberTuple = ['Hello', 1, 2, 3];
4const tuple2: StringNumberTuple = ['World', 4, 5, 6, 7];
5
6console.log(tuple1); // Output: ['Hello', 1, 2, 3]
7console.log(tuple2); // Output: ['World', 4, 5, 6, 7]

In the example above, StringNumberTuple represents a tuple type with a string as the first element, followed by any number of additional numbers. This allows you to create tuples with a variable number of elements beyond the first one.

In Closing

Tuple types provide a flexible and type-safe way to work with fixed-length arrays that have different data types for each element. By leveraging tuple types in TypeScript, you can ensure the correct structure and type safety of your data, function arguments, and return values. Start using tuple types in your TypeScript projects today and benefit from their ability to represent structured data!