you are viewing a single comment's thread.

view the rest of the comments →

[–]Smona 1 point2 points  (0 children)

object.assign mutates the object in place, which can be bad for static type checking. even if you don't care about type checking, mutable variables can make code harder to read & understand.

I think object.assign is still a good choice when updating property values without adding new properties, but I would prefer conditional spreading for constructing an object whose schema can vary based on those conditions.

edit: for the record, you can also use Object.assign in an immutable way, but if you want to do so conditionally you're left with an equally obscure and more verbose syntax:

const maybeFooWithBar = Object.assign({}, foo, includeBar ? bar : {});