Title: Incorrect operator precedence when using private fields with nullish coalescing assignment for targets <= es2021 · Issue #61109 · microsoft/TypeScript · GitHub
Open Graph Title: Incorrect operator precedence when using private fields with nullish coalescing assignment for targets <= es2021 · Issue #61109 · microsoft/TypeScript
X Title: Incorrect operator precedence when using private fields with nullish coalescing assignment for targets <= es2021 · Issue #61109 · microsoft/TypeScript
Description: 🔎 Search Terms "nullish coalescing assignment", "private properties", "private fields", "target es2021", "operator precedence" 🕗 Version & Regression Information This changed between versions 3.9.7 and 4.0.5 (with the introduction of nul...
Open Graph Description: 🔎 Search Terms "nullish coalescing assignment", "private properties", "private fields", "target es2021", "operator precedence" 🕗 Version & Regression Information This changed between versions 3.9.7...
X Description: 🔎 Search Terms "nullish coalescing assignment", "private properties", "private fields", "target es2021", "operator precedence" 🕗 Version & Regr...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/61109
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Incorrect operator precedence when using private fields with nullish coalescing assignment for targets \u003c= es2021","articleBody":"### 🔎 Search Terms\n\n\"nullish coalescing assignment\", \"private properties\", \"private fields\", \"target es2021\", \"operator precedence\"\n\n### 🕗 Version & Regression Information\n\n- This changed between versions 3.9.7 and 4.0.5\n\n(with the introduction of nullish coalescing assignment)\n\n### ⏯ Playground Link\n\nhttps://www.typescriptlang.org/play/?noUnusedLocals=true&noUnusedParameters=true&target=8&jsx=0&noFallthroughCasesInSwitch=true&useUnknownInCatchVariables=true&ts=5.8.0-dev.20250204&ssl=6&ssc=1&pln=9&pc=1#code/MYGwhgzhAEDCIwN4ChpugBwK4CMQEtgAFAJwHsMAuaAOywFscBTE6AH2ixoBMmAzfDSbcA3KnQBiDCXwA3MABcmpCtTqMW7Tj36DhY8WhpkVeJvQAUASmgp096AHpH0ACoALfDADuZEgGsYSGgmAA8MJmAlbmolEhowEgBPEPCSJih8MhpoMB5oBXcmWiwQAgh3aGAyMBAM4EEAc0MHZ1zMxpp6JhoFXPSQ+RAsRWFoGUb3BQBaBTJpur4FABpoCDICooKWBOTU6QyILJyvFvs2xvIsCO5c-K9obJAUpiGR6OhvIpyAA0KvAB02DwhBUGB+0AedDKXncALO6H+ECBuAIxHIGGgAH4sQBeaB8WoQYpY2ivFgeLzWaDUACMACYAMxiBzoNqUnx+QKrZjAMBYYmfYoC4oYRI9QoZDKUBFoJEAqQyeRKMHYvHQCyEhAksmyCmeCDU6j0gAMVhZ6AAvsgWtIyGZLDY7KynC4OQSwPgEDzIvzBZLoD8cbiIQ8FCQ8hBwB85rlAziIV9CJV3MEABIASQA4mmAKIAJVlrsw6WAwh6Zc2eU2xTiuxeaUOx3hLs2gMVclGquDHu12N1+qpNmNJotaGt1uQ1Ro6zqANATES1gM04gfVY+KE3jgCDEJABxlMdUde4PJnIDuXNtXZDnIDIjQsAHJjCX7ce1htCSQn+abfu7UvP8p2yWcmABe9HxfDZAI-RRcjKX8DD4LgomOAcSA5aw1HJVhnTldxyG3LdoFzEhyBIZ93QqMhSludJamecZFzKFIhD1VhUwwCIaAAQiQ5BLSAA\n\n(BTW: run it to see the problem)\n\n### 💻 Code\n\n```ts\nclass Cls {\n publicProp: number | undefined;\n #privateProp: number | undefined;\n\n noProblem() {\n // This works as expected: ternary expression and the nullish coalescing\n // assignment are evaluated right-to-left, so the ternary expression is\n // grouped and is only evaluated when `this.publicProp` is nullish.\n this.publicProp ??= false ? neverThis() : 123;\n // This works, because we use parentheses:\n this.#privateProp ??= (false ? neverThis() : 20);\n }\n\n problem() {\n // This fails, because the `??=` is translated to a `??` which has HIGHER\n // precedence than the ternary expression.\n this.#privateProp ??= false ? neverThis() : 20;\n }\n}\n\nconsole.clear();\n\nconst r = new Cls;\nr.noProblem();\nr.noProblem();\n\nconsole.log('no problem so far');\n\nr.problem();\n\nconsole.log('no problem at all');\n\nfunction neverThis(): never {\n throw new Error('This should really really never happen!');\n}\n```\n\n\n### 🙁 Actual behavior\n\nThe statement `this.#privateProp ??= false ? neverThis() : 20;` is translated into:\n```js\n__classPrivateFieldSet(this, _Cls_privateProp, __classPrivateFieldGet(this, _Cls_privateProp, \"f\") ?? false ? neverThis() : 20, \"f\");\n```\n\nThe relevant piece here is:\n```js\n__classPrivateFieldGet(this, _Cls_privateProp, \"f\") ?? false ? neverThis() : 20\n```\n\nwhich is equivalent to:\n```js\n(__classPrivateFieldGet(this, _Cls_privateProp, \"f\") ?? false) ? neverThis() : 20\n```\n\nBut it should have been:\n```js\n__classPrivateFieldGet(this, _Cls_privateProp, \"f\") ?? (false ? neverThis() : 20)\n```\n\n### 🙂 Expected behavior\n\nExpected correct operator precedence in transpiled code.\n\n### Additional information about the issue\n\nThis is no issue on targets greater than es2021, because of the native support of private fields.","author":{"url":"https://github.com/pavadeli","@type":"Person","name":"pavadeli"},"datePublished":"2025-02-04T08:54:14Z","url":"https://github.com/microsoft/TypeScript/issues/61109"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:7ddae0f1-87d6-d525-d8ab-a257cfea674e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D62E:1F7384:9FBF7A:D4FE41:6B00E7E0 |
| html-safe-nonce | 7ef20596674ea57130879d8a2ecc768d21a8d55eb68c0d11ac097e118d55d9c1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENjJFOjFGNzM4NDo5RkJGN0E6RDRGRTQxOjZCMDBFN0UwIiwidmlzaXRvcl9pZCI6IjcyMjU1NTUyNDQ4NjI3MjgxNjAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | ef748857db279e820640a82cbf1be4ce11fd60b786376945b14df82daaddc298 |
| hovercard-subject-tag | issue:2829518903 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/61109/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1d53bd12ef87c0d222ee05ef5c095267a0cd3e122348fa684f78a93b9d68afed/microsoft/TypeScript/issues/61109 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1d53bd12ef87c0d222ee05ef5c095267a0cd3e122348fa684f78a93b9d68afed/microsoft/TypeScript/issues/61109 |
| og:image:alt | 🔎 Search Terms "nullish coalescing assignment", "private properties", "private fields", "target es2021", "operator precedence" 🕗 Version & Regression Information This changed between versions 3.9.7... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | pavadeli |
| hostname | github.com |
| expected-hostname | github.com |
| None | 27088b6977484d0d3321d9a11c907c6d6637b315cfa359ef7952dbf6292f9b14 |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 624179fb8b88d26ba5bba08894eb60a1c5a1d93d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width