There are already a lot of cool features in ES6/ES7 for defining Javascript objects. However, the following pattern is common in Javascript:
const obj = {
requiredKey1: ...,
requiredKey2: ...
};
if (someCondition) {
obj.optionalKey1 = ...;
}
Is there a way to define the object all at once with both optional and required keys?
optionKey1: someCondition ? value : undefined
? – Endermic{ key?: optionalValue }
or with property shorthand:{ optionalValue? }
– Heaver