3 ms·
>That's funny, a good portion of the article is about the unsafety of destructuring... Destructuring is guaranteed to be 100% safe if you do it properly. That'
by ramesh31 4y ago
>That's funny, a good portion of the article is about the unsafety of destructuring...
Destructuring is guaranteed to be 100% safe if you do it properly. That's the whole point.
const envelope = {data:{edge:{node:{value:'test'}}}}
const {data: {edge: {node: {value = ''} = {} } = {} } = {} } = envelope || {}
console.log(value)
Is completely safe. Versus the alternative of using an accessor library or null checking at each level.
You can also do it with arrays to avoid out of range errors:
const arrayVals = ['test']
const {0: firstEl = ''} = arrayVals || []
console.log(firstEl)
100% safe and avoids mutating the array