Typescript currently supports unary logical operators of the form x += yand x -= y, that are syntactically equivalent to x = x + y and x = x - y.
I would like to see the introduction of the logical equivalent of these operators, say &&= and ||=.
Expressions that take the form x = x && y and x = x || y could be expressed more succinctly by x &&= y and x ||= y respectively, using the proposed operators.
It is often the case that deeply nested properties in an object tree need to be assigned a fallback using the || operator. The latter operator, being binary, would require repeating the property drill-down expression.
foo.bar.baz = foo.bar.baz || "default";
With the proposed operators, this could be expressed more succinctly as
foo.bar.baz ||= "default";
The operators &&= and ||=would hence by syntactic sugar for the binary equivalents && and || where the result of the expressions are assigned to the first operator in the original binart expression. That is:
x &&= y is syntactically equivalent to x = x && y
x ||= y is syntactically equivalent to x = x || y
All rules for the equivalent expressions using the existing binary operators apply.
Please note that no change is required to existing operators or other language features.
My suggestion meets these guidelines:
Looks like you are talking about logical assignment operators, currently a Stage 1 Proposal for introduction into ECMAScript. TypeScript doesn't generally implement new language features until they reach Stage 3. I think if you want to see movement on this you'd have better luck participating in the ECMAScript proposal process. Cheers!
@jcalz indeed. I was using the wrong terms and failed to find the proposal. Thanks for the links!
[Y] This feature would agree with the rest of TypeScript's Design Goals.
It would violate goal 8:
Avoid adding expression-level syntax.
yes but now, it's in stage 3:)
https://github.com/tc39/proposal-logical-assignment
--- Update Aug 6th
it's in stage 4 right now
It's in stage 3!!!
yes but now, it's been in stage 3:)
https://github.com/tc39/proposal-logical-assignment
Seems to be different from my proposal though, where a ||= b is equivalent to a = a || b and a &&= b is equivalent to a = a && b
@RyanCavanaugh do you think this can be reopened?
Babel shipped support for it in preset-env a few days ago
What's the status of this? This is now supported in Firefox, Chrome, and Safari
a = (a && b) and a && (a = b) in which case only the latter is being supported)
Most helpful comment
yes but now, it's in stage 3:)
https://github.com/tc39/proposal-logical-assignment
--- Update Aug 6th
it's in stage 4 right now