TypeScript Object Tuple Types
Last Updated :
24 Apr, 2025
TypeScript provides various features to enhance type safety and developer productivity. One of these features includes Object Tuple Types. This feature ensures that an object follows a consistent shape in our code which results in a reduction in runtime errors and improves code quality.
What are Object Tuple Types?
- Object Tuple Types allow us to define structured objects with a fixed number of properties, each with a specific data type.
- Object Tuple Types in TypeScript are a way to define the structure of objects as a tuple. A tuple is an ordered list of elements and in TypeScript, a Tuple represents an object with a specific number of properties i.e. have a fixed length, each index having a predefined data type.
- Unlike JavaScript objects, Object tuple types have a fixed structure.
- Tuple types in TypeScript are designed to have a fixed and specific structure with a predefined number of elements and data types. Therefore, the TypeScript compiler enforces that you provide all the expected values during object creation.
- If we don't provide all the expected values while creating an object of a Tuple type in TypeScript, we will get a compilation error.
Syntax:
type TupleTypeName = [Type1, Type2, ...];
Where -
- TupleTypeName: The name you assign to the tuple type.
- Type1, Type2, ...: The data types of each property in the tuple.
Tuple Array
- In Tuple Array, we create an array of tuple types.
- Here, Tuple Types represent a sequence of elements having a fixed structure and each element has its own specific predefined data type.
- This can ensure that each tuple in the array has a specific structure(length and property).
- This can allow us to maintain a collection of data structure.
Example: Here, we declared a tuple type item first and then we created an array of Products of item[] tuple type.
JavaScript
type item = [string, number];
let Products: item[] = [
["Laptop", 999],
["Smartphone", 599],
["Tablet", 299],
];
console.log(Products);
Output:
[ [ 'Laptop', 999 ], [ 'Smartphone', 599 ], [ 'Tablet', 299 ] ]
Destructure Tuple
- Destructuring is a feature that allows us to break the tuple and assign the tuple values to different variables.
- This feature allows easier access to tuple values after extracting and assigning them to a variable from the tuple.
- If you want to know about destructing in depth please refer to this article: How tuple destructuring works in TypeScript?
Example: Here, we declare a type Triangle create a variable of type Triangle and assign the values to the tuple. To Destructure this tuple and get the values into individual elements, we create variables in this form [a, b, c], so the values will assigned in this manner: let a=10, b=20, c=30.
JavaScript
// Define a triangle tuple type
type Triangle = [number, number, number];
// Create a triangle variable of Type Triangle
let triangle: Triangle = [10, 20, 30];
// Destructure the tuple into individual variables a, b, c
let [a, b, c] = triangle;
// Calculate the perimeter of the Triangle
let perimeter = a + b + c;
console.log(`Perimeter of triangle with
sides ${a} ,${b}, ${c} : ${perimeter}`);