How tuple destructuring works in TypeScript ?
Last Updated :
16 Jun, 2022
Tuples in TypeScript are basically a simple Array with definite length and definite Data-type. Destructuring means breaking the structure. In this article we will see how Destructuring of tuple in TypeScript work.
Destructuring is simply breaking up into part and assigning to variables. Tuple elements are breaking into it’s part. we can break the Tuple with the help of assignment operator.
Example: Simple example of tuple destructuring.
Javascript
let Student = [ "Aman" , "Arun" , "Bimal" ];
let stud1 = Student[0];
let stud2 = Student[1];
let stud3 = Student[2];
|
In this tuple destructuring we simply manually break the value of tuple and assign it to variable. In destructuring, we basically assign the value of each element of tuple to some variable. This is the basic example of destructuring of a tuple. We have some more destructuring syntax. let see how these work.
How Different Destructuring of Tuple works?
We have one way for tuple destructuring which is as follows:
let Student_roll = [ 1001, 1002, 1003 ];
let [stud1, stud2, stud3 ] = Student_roll;
Above code is equivalent to the following code:
var stud1=1001, stud2=1002, stud3=1003;
We can write the Destructuring of tuple of name of students for variable as student id with the above illustrated approach. This destructuring actually helps in assigning the value of tuple with 0 index to stud1 and so on. At the end it is same as previous code which is of manually assigning the value to variable.
Javascript
let Student = [ "Aman" , "Arun" , "Bimal" ];
let [stud1, stud2, stud3 ] = Student;
console.log(stud1);
console.log(stud2);
console.log(stud3);
|
Output:
Aman
Arun
Bimal
At this point we have no problem but what if destructuring one element at a time with this approach?. Now we will see how ignoring of element work:
We have way to ignore the middle value in destructuring Tuple. Let see how it’s work.
let Student = [ "Aman", "Arun", "Bimal" ];
let [,,stud3 ] = Student;
Here “,” is used for the ignoring the value in tuple. This code is equivalent to following code:
let stud3 = "Bimal";
In this code we are using “,” which is used for ignoring the variable. So if we want to skip the 1st element in tuple we can write code as follow:
Javascript
let Student = [ "Aman" , "Arun" , "Bimal" ];
let [, stud1, stud2 ] = Student;
console.log(stud1);
console.log(stud2);
|
Output: And now if we print the variable stud1 and stud2 It will print follow.
Arun
Bimal
We also have spreading operator in destructuring tuple in TypeScript let’s look how it’s work.
let Student_roll = [ 1001, 1002, 1003 ];
let [...stud3 ] = Student_roll;
Here …stud3 define the slicing of tuple and if we use spread operator with 0 indexed variable in destructuring it slice from the 0 to the end of tuple and assign to the variable. which is equivalent to following code:
let stud3 = Student_roll.slice(0);
We can use this operator to separate the first element and all remaining element and store both of them in different variable. Here it will slice with 1 index value.
Javascript
let Student = [ "Aman" , "Arun" , "Bimal" ];
let [stud1, ...remaining_class ] = Student;
console.log(stud1);
console.log(remaining_class);
|
Output:
Aman
["Arun", "Bimal"];
Example: In this example we will creating a tuple with the actual TypeScript syntax (including the data types wherever required) and then further will perform the tuple destructuring.
Javascript
let fruits : string[] = [ "Apple" , "Banana" , "Mango" ];
let [fruit_1, fruit_2, fruit_3]: string[] = fruits;
console.log(fruit_1 + " - " + fruit_2 + " - " + fruit_3);
|
Output:
Apple - Banana - Mango
Reference: https://www.typescriptlang.org/docs/handbook/variable-declarations.html#tuple-destructuring
Similar Reads
How optional chaining works in TypeScript ?
In this article, we will try to understand how we could perform as well as analyze the working of optional chaining in TypeScript. TypeScript Optional Chaining: TypeScript Optional Chaining is the process of searching and calling variables, methods, parameters that might be nil in existence.This par
3 min read
What is Parameter Destructuring in TypeScript ?
Parameter destructuring in TypeScript is a way to extract values from objects or arrays passed as function parameters, making it easier to work with their properties or elements directly within the function body. There are several methods through which parameter destructuring is achieved in TypeScri
3 min read
How arrays works in TypeScript ?
Array in TypeScript is just similar to an array in other programming languages. The array contains homogeneous values means a collection of multiple values with different data types. TypeScript array is also fixed size, can not be resized once created. An array is a type of data structure that store
2 min read
How TypeScript Compilation Works ?
TypeScript is a superset of JavaScript that adds type safety to your code. It compiles into plain JavaScript, allowing it to run in any JavaScript environment. The TypeScript compiler (tsc) checks the code for errors and then converts it into JavaScript. During this process, all TypeScript-specific
4 min read
How to use Type Guards in TypeScript ?
Here are the methods to use type guards in TypeScript: 1. Using typeof Type GuardsThe typeof operator checks the type of a variable, primarily for primitive types like string, number, boolean, etc. [GFGTABS] JavaScript function processValue(value: string | number) { if (typeof value === 'string
3 min read
What is Type Erasure in TypeScript?
TypeScript is a very mighty superset of JavaScript, which adds static typing to the language and allows developers to catch errors early on as well as write more maintainable code. This being said a TypeScript type system erases type information at compile time (or during the compilation), a phenome
4 min read
How to use getters/setters in TypeScript ?
In TypeScript, getters and setters provide controlled access to class properties, enhancing encapsulation and flexibility. Getters allow you to retrieve the value of a property with controlled logic.Setters enable controlled assignment to properties, often including validation or transformations.[GF
5 min read
How to Check Types in Typescript?
Checking types in TypeScript involves methods like typeof for primitive types, instanceof for class instances, and custom type guards for complex type validation. These techniques help ensure variables are correctly typed, improving code safety, and readability, and preventing runtime errors.Here ar
3 min read
Destructuring of Props in ReactJS
Destructuring is a simple property that is used to make code much clear and readable, mainly when we pass props in React. Table of Content What is Destructuring?Advantages of DestructuringHow to use Destructuring?Using this.props methodUsing the Extraction methodUsing the Re-assigning methodSummaryW
4 min read
Mapping Distributive Types in TypeScript
The type system of TypeScript is considered to be quite expressive and, like all other languages, contains an interesting feature known as distributive types, the concept of distributive types allows one to perform operations of distributing conditional types over union types, which is extremely use
6 min read