3 ms·
Nah. Destructure all the things. The alternative is writing unsafe code or using a library in place of standard language features.
by ramesh31 4y ago
Nah. Destructure all the things. The alternative is writing unsafe code or using a library in place of standard language features.
- klyrs 4y agoThat's funny, a good portion of the article is about the unsafety of destructuring...
- 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