Skip to content

Logical Unary Assignment Operators &&= and ||= #31011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
colinvella opened this issue Apr 18, 2019 · 10 comments
Closed

Logical Unary Assignment Operators &&= and ||= #31011

colinvella opened this issue Apr 18, 2019 · 10 comments
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript

Comments

@colinvella
Copy link

colinvella commented Apr 18, 2019

Search Terms

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

Suggestion

Typescript currently supports unary logical operators of the form x += y and 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.
@jcalz
Copy link
Contributor

jcalz commented Apr 18, 2019

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!

@colinvella colinvella changed the title Logical Unary Operators &&= and ||= Logical Unary Assignment Operators &&= and ||= Apr 18, 2019
@colinvella
Copy link
Author

@jcalz indeed. I was using the wrong terms and failed to find the proposal. Thanks for the links!

@MartinJohns
Copy link
Contributor

[Y] This feature would agree with the rest of TypeScript's Design Goals.

It would violate goal 8:

Avoid adding expression-level syntax.

@RyanCavanaugh RyanCavanaugh added Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript labels Apr 18, 2019
@Kingwl
Copy link
Contributor

Kingwl commented Mar 31, 2020

Duplicate #37255

@PassionPenguin
Copy link

PassionPenguin commented Apr 24, 2020

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

@julianeden
Copy link

It's in stage 3!!!

@colinvella
Copy link
Author

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

@tom-sherman
Copy link

@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

@vegerot
Copy link

vegerot commented Aug 7, 2020

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

@jcalz
Copy link
Contributor

jcalz commented Aug 7, 2020

#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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

9 participants