3 ms·
Hurrah for object spreads! Time to go grepping for calls to _.default and _.extend.
by ng12 10y ago
Hurrah for object spreads! Time to go grepping for calls to _.default and _.extend.
- WorldMaker 10y agoAlso, Object.assign
- Sankozi 10y agoUnfortunately 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.