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}`);


Output:

Perimeter of triangle with sides 10 ,20, 30 : 60

TypeScript Object Tuple Types

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.

Similar Reads

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....

Destructure Tuple

...

Optional 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?...

Tuple with Rest Elements

...

Tuple with Rest Parameters and Arguments

In Optional Tuple, We can make a property optional, which means this property may or may not be present in the tuple. This feature provides us great flexibility while writing code as in tuple type we have a fixed length and all properties are mandatorily required while creating a variable of tuple type. But using this feature, we can make a property of tuple type optional....