Typescript named object of destructuring

Is it possible to destroy an object with the names of user variables?

TypeScript

const { top } = { top: 1000 };

Javascript

var top = { top: 1000 }.top;

But I want something like the one shown below (doesn't work).

TypeScript

const { top as elementTop } = { top: 1000 };

Javascript

var elementTop = { top: 1000 }.top;
+4
source share
2 answers

The correct syntax is:

const { top: elementTop } = { top: 1000 };

Reference

+3
source

This is ES6 destructuring when you need to assign new variable names:

var o = {p: 42, q: true};

var {p: foo, q: bar} = o;

In your example, this would be:

const { top: elementTop } = { top: 1000 }; 

Additional information: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

+3
source

All Articles