Javascript ES6 spread operator on undefined [duplicate]
Solution 1:
This behavior is useful for doing something like optional spreading:
function foo(options) {
const bar = {
baz: 1,
...(options && options.bar) // options and bar can be undefined
}
}
And it gets even better with optional chaining, which is in Stage 4
now (and already available in TypeScript 3.7+):
function foo(options) {
const bar = {
baz: 1,
...options?.bar //options and bar can be undefined
}
}
a thought: its too bad it doesn't also work for spreading into an array