Operator proposal: spread-assignment preserving object fields

I would like to suggest a new operator that would be a shorthand for a composition of the the operators: assigment and spread.

This would help with extending existing objects or assigning objects only partially with a shorter syntax than Object.assign( .. ) or spreading the object inside a new object and overwriting selected key values, as shown in the example. The feature would be extremely helpful for React.js developers who use such an operation frequently to change the component's state.

Suppose we have let obj = { a: 1, b: 2, c: () => 3 }. Now, to modify the object by re-assigning a massive amount of properties (in this example simplified to just a few) one would do: obj = { ...obj, b: "!", c: () => 4 }. What I would suggest to change this would be to do a partial assign, e.g. with a ...= syntax, which would look like the following: obj ...= { b: "!", c: () => 4 }.

Even further, there might be a consideration to add a similar operator extending the key-value colon operator: partiallyAssignedObject = { a ...: { partial property1: 3, partialProperty3: "#" }.

Are you looking to modify the object, or overwrite the obj variable with an altered copy? The former could be done with Object.assign, the latter doesn't seem to be such a frequent operation to me, usually you would do something else with the new object.

It is normally expected that compound assignment operators desugar to the respective binary operator - in your case obj = obj ... { b: "!", c: () => 4 }. I think we would need this first, and I don't think it should use the ... token.

What benefit does this yield that Object.assign doesn't (other than not mutating the original instance)? Also, there's two other things to consider independent of that:

  1. What would the syntax a ...= b do if b is an object?
  2. How would arrays be accomodated? And if they use the same syntax, what would happen if b is an array and a is an object, or if a is an array and b is an object?

I intend to reassign obj to an altered copy. I know there is an alternative way to modify it in-place with Object.assign, but the only aim of the operator I suggested would be to have a shorthand for such an operation, which may be based on one of the two ways.

I am not quite sure what You mean by saying this, but the problem I wanted to state here was that assigning to a nested object would also overwrite it with just the = operator, not preserving the existing fields, so it would be helpful to have a shorthand operator shorter than spreading the original in the newly-created object.

It does not yield any optimialization benefits, but would just be a sugary syntax that would make it shorter to alter object properties without having to write an additional spread line at the beginning of the new object to be assigned, or without using the long Object.assign(...) syntax. The underlying implementation itself could even be the same as for Object.assign, I am just thinking of a syntactic shorthand to make the code smaller and more readable.