Typescript: Logical Unary Assignment Operators &&= and ||=

Created on 18 Apr 2019  路  10Comments  路  Source: microsoft/TypeScript

Search Terms

  • unary
  • unary operators
  • unary logical operators
  • logical assignment
  • &&=
  • ||=

Suggestion

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 ||=.

Use Cases

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.

Examples

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.

Checklist

My suggestion meets these guidelines:

  • [Y] This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • [Y] This wouldn't change the runtime behavior of existing JavaScript code
  • [Y] This could be implemented without emitting different JS based on the types of the expressions
  • [Y] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • [N] This feature would agree with the rest of TypeScript's Design Goals.
Out of Scope Suggestion

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

All 10 comments

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

https://babeljs.io/blog/2020/07/30/7.11.0

What's the status of this? This is now supported in Firefox, Chrome, and Safari

37727 fixes #37255, which this issue duplicates (unless you care about the difference between a = (a && b) and a && (a = b) in which case only the latter is being supported)

Was this page helpful?
0 / 5 - 0 ratings