Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES6: destructuring an object with symbols as keys

I have an object that contains symbols as keys. How do I do destructuring assignment in this case?

let symbol = Symbol()
let obj = {[symbol]: ''}
let { /* how do I create a variable here, that holds the value of [symbol] property? */ } = obj

I need to know if this possible, I do know the obvious and simple workarounds, but that's not what I am asking.

UPD. Funny enough I knew how to do that but typescript produced errors, and I thought I did something wrong in JS. Here's a fix for typescript users.

like image 826
Nurbol Alpysbayev Avatar asked Dec 23 '22 02:12

Nurbol Alpysbayev


1 Answers

Use an alias (see assigning to new variable names):

let symbol = Symbol()
let obj = { [symbol] : 'value'}
let { [symbol]: alias } = obj

console.log(alias)
like image 95
Ori Drori Avatar answered Jan 05 '23 08:01

Ori Drori