4 ms·
Unfortunately newThing = {...thing, propertyName: value} Will always compile, even if propertyName is invalid. The only advantage over Object.assign I see i
by Sankozi 10y ago
Unfortunately
newThing = {...thing, propertyName: value}
Will always compile, even if propertyName is invalid. The only advantage over Object.assign I see is IDE support (at least in Intellij IDEA). I will still use:
newThing = Object.assign({}, thing);
newThing.propertyName = value;
This will not compile if propertyName is invalid and is IDE friendly.
- WorldMaker 10y agoYou should be able to add a type requirement and get the error you are expecting. Instead of this: let newThing = { ...thing, propertyName: value } // type of newThing would be deduced: TypeOfThing & { propertyName: TypeOfValue } If you add the type requirement the compiler should balk on the propertyName: let newThing: TypeOfThing = { ...thing, propertyName: value } ETA: You'd have the same lack of compiler error if you were instead using: let newThing = Object.assign({}, thing, { propertyName: value }) Because Object.assign will also build intersection types by default.