DEV Community

kaustav karmakar
kaustav karmakar

Posted on

Destructuring Assignment is a Typescript

Destructuring
TypeScript supports the following forms of Destructuring (literally named after de-structuring i.e. breaking up the structure):
1.
Object Destructuring
2.
Array Destructuring
It is easy to think of destructuring as an inverse of structuring. The method of structuring in JavaScript is the object literal:

var foo = {
    bar: {
        ba
s: 123
    }
};
Enter fullscreen mode Exit fullscreen mode

Without the awesome structuring support built into JavaScript, creating new objects on the fly would indeed be very cumbersome. Destructuring brings the same level of convenience to getting data out of a structure.
Object Destructuring
Destructuring is useful because it allows you to do in a single line, what would otherwise require multiple lines. Consider the following case:

var rect = { x: 0, y: 10, width: 15, height: 20 };
Enter fullscreen mode Exit fullscreen mode


// Destructuring assignment

var {x, y, width, height} = rect;
console.log(x, y, width, height); // 0,10,15,20
​
rect.x = 10;
({x, y, width, height} = rect); // assign to existing variables using outer parentheses
console.log(x, y, width, height); // 10,10,15,20
Enter fullscreen mode Exit fullscreen mode

Here in the absence of destructuring you would have to pick off x,y,width,height one by one from rect.
To assign an extracted variable to a new variable name you can do the following:
// structure

const obj = {"some property": "some value"};
Enter fullscreen mode Exit fullscreen mode


// destructure

const {"some property": someProperty} = obj;
console.log(someProperty === "some value"); // true
Enter fullscreen mode Exit fullscreen mode

Additionally you can get deep data out of a structure using destructuring. This is shown in the following example:

var foo = { bar: { bas: 123 } };
var {bar: {bas}} = foo; // 
Enter fullscreen mode Exit fullscreen mode

Effectively

var bas = foo.bar.bas;
Enter fullscreen mode Exit fullscreen mode

Object Destructuring with rest
You can pick up any number of elements from an object and get an object of the remaining elements using object destructuring with rest.

var {w, x, ...remaining} = {w: 1, x: 2, y: 3, z: 4};
console.log(w, x, remaining); // 1, 2, {y:3,z:4}
Enter fullscreen mode Exit fullscreen mode

A common use case is also to ignore certain properties. For example:
// Example function

function goto(point2D: {x: number, y: number}) {
Enter fullscreen mode Exit fullscreen mode

// Imagine some code that might break
// if you pass in an object
// with more items than desired
}
// Some point you get from somewhere

const point3D = {x: 1, y: 2, z: 3};
Enter fullscreen mode Exit fullscreen mode

/** A nifty use of rest to remove extra properties */

const { z, ...point2D } = point3D;
goto(point2D);
Enter fullscreen mode Exit fullscreen mode

Array Destructuring
A common programming question: "How to swap two variables without using a third one?". The TypeScript solution:

var x = 1, y = 2;
[x, y] = [y, x];
console.log(x, y); // 2,1
Enter fullscreen mode Exit fullscreen mode

Note that array destructuring is effectively the compiler doing the [0], [1], ... and so on for you. There is no guarantee that these values will exist.
Array Destructuring with rest
You can pick up any number of elements from an array and get an array of the remaining elements using array destructuring with rest.

var [x, y, ...remaining] = [1, 2, 3, 4];
console.log(x, y, remaining); // 1, 2, [3,4]
Enter fullscreen mode Exit fullscreen mode

Array Destructuring with ignores
You can ignore any index by simply leaving its location empty i.e. , , in the left hand side of the assignment. For example:

var [x, , ...remaining] = [1, 2, 3, 4];
console.log(x, remaining); // 1, [3,4]
Enter fullscreen mode Exit fullscreen mode

JS Generation
The JavaScript generation for non ES6 targets simply involves creating temporary variables, just like you would have to do yourself without native language support for destructuring e.g.

var x = 1, y = 2;
[x, y] = [y, x];
console.log(x, y); // 2,1
Enter fullscreen mode Exit fullscreen mode


// becomes //

var x = 1, y = 2;
_a = [y,x], x = _a[0], y = _a[1];
console.log(x, y);
var _a;
Enter fullscreen mode Exit fullscreen mode

Summary
Destructuring can make your code more readable and maintainable by reducing the line count and making the intent clear. Array destructuring can allow you to use arrays as though they were tuples
Please give a like and follow,if you like my work

Top comments (2)

Collapse
 
kaustavkarmakar2 profile image
kaustav karmakar
Collapse
 
kaustavkarmakar2 profile image
kaustav karmakar