Spread syntax ES6 with statement

Viewed 8835

I tried to write ternary operator with spread syntax and copy two objects. Is it possible to use ternary operator with spread syntax inside with literal objects? My code works okay, I just want to optimize it.

hintStyle: disabled ? {...globalStyles.hint, ...globalStyles.hintDisabled} : globalStyles.hint,

I want to write like this:

hintStyle: {...globalStyles.hint, {disabled ? ...globalStyles.hintDisabled : {}}},
2 Answers

I ran into this same problem today, my use case was simple enough that I could do this:

// given any-typed parameters a and b, I want to append b to a
// if a is iterable, I want to use spread.
// Initially I had:

const fn1 = (a, b) => [Symbol.iterator in a ? ...a : a, b]

// This was my solution:

const fn2 = (a, b) => Symbol.iterator in a ? [...a, b] : [a, b];
Related