Comment on Safe AssignmentComments−spankalee1yThe real safe assignment that I want to see is for optional chaining to be valid in LHS of assignments: foo?.bar = 42; This would not perform the assignment if an optionally chained value was nullish. It downlevels to foo == null ? undefined : foo.bar = 42; Given that the assignment is a SyntaxError now, this should be possible.−nuxi1yYou can avoid the SyntaxError by abusing boolean expressions a bit: > obj = {} {} > obj != null && (obj.bar = 42) 42 > obj { bar: 42 } Not as pretty as your LHS case, but it works.−Wingy1yWith the length of that you might as well do: if (obj) obj.bar = 42−gqcwwjtg1yWould it evaluate the RHS? It feels like it shouldn’t, but somehow that seems weird too.
Comments
The real safe assignment that I want to see is for optional chaining to be valid in LHS of assignments:
This would not perform the assignment if an optionally chained value was nullish. It downlevels to Given that the assignment is a SyntaxError now, this should be possible.You can avoid the SyntaxError by abusing boolean expressions a bit:
Not as pretty as your LHS case, but it works.With the length of that you might as well do:
Would it evaluate the RHS? It feels like it shouldn’t, but somehow that seems weird too.