Title: Manually constructed nullish coalescing AST is missing parentheses when printed · Issue #61369 · microsoft/TypeScript · GitHub
Open Graph Title: Manually constructed nullish coalescing AST is missing parentheses when printed · Issue #61369 · microsoft/TypeScript
X Title: Manually constructed nullish coalescing AST is missing parentheses when printed · Issue #61369 · microsoft/TypeScript
Description: 🔎 Search Terms nullish coalescing parentheses ast 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about nullish coalescing ⏯ Playground Link https://stackblitz.com/edit...
Open Graph Description: 🔎 Search Terms nullish coalescing parentheses ast 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about nullish coalescing ⏯ Pla...
X Description: 🔎 Search Terms nullish coalescing parentheses ast 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about nullish coalescing ⏯...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/61369
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Manually constructed nullish coalescing AST is missing parentheses when printed","articleBody":"### 🔎 Search Terms\n\nnullish coalescing parentheses ast\n\n### 🕗 Version & Regression Information\n\n- This is the behavior in every version I tried, and I reviewed the FAQ for entries about nullish coalescing\n\n\n### ⏯ Playground Link\n\nhttps://stackblitz.com/edit/vitejs-vite-letuvqes?file=src%2Fmain.ts\n\n### 💻 Code\n\n```ts\nimport * as ts from 'typescript';\n\nconst printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });\nconst src = ts.createSourceFile('test.ts', '', ts.ScriptTarget.Latest);\n\nconst nullishNode = ts.factory.createExpressionStatement(\n ts.factory.createBinaryExpression(\n ts.factory.createConditionalExpression(\n ts.factory.createIdentifier('a'),\n ts.factory.createToken(ts.SyntaxKind.QuestionToken),\n ts.factory.createIdentifier('b'),\n ts.factory.createToken(ts.SyntaxKind.ColonToken),\n ts.factory.createIdentifier('c')\n ),\n ts.factory.createToken(ts.SyntaxKind.QuestionQuestionToken),\n ts.factory.createIdentifier('d')\n )\n);\n\nconst orNode = ts.factory.createExpressionStatement(\n ts.factory.createBinaryExpression(\n ts.factory.createConditionalExpression(\n ts.factory.createIdentifier('a'),\n ts.factory.createToken(ts.SyntaxKind.QuestionToken),\n ts.factory.createIdentifier('b'),\n ts.factory.createToken(ts.SyntaxKind.ColonToken),\n ts.factory.createIdentifier('c')\n ),\n ts.factory.createToken(ts.SyntaxKind.BarBarToken),\n ts.factory.createIdentifier('d')\n )\n);\n\n// a ? b : c ?? d\nconst nullish = printer.printNode(ts.EmitHint.Unspecified, nullishNode, src);\n// (a ? b : c) || d\nconst or = printer.printNode(ts.EmitHint.Unspecified, orNode, src);\n```\n\n\n### 🙁 Actual behavior\n\nWhen I manually create AST for the expression `(a ? b : c) ?? d` but leave the parentheses out of the AST, the generated code does not add the parens back in and therefore does not match the structure of the AST.\n\nWhen I do this same thing with `||` or `&&` it behaves as expected, inserting the parens back in to the generated code.\n\n### 🙂 Expected behavior\n\nThe `nullishNode` should be printed in the same way the `orNode` is in the example, adding back in the parentheses so that the code matches the structure of the AST\n\n### Additional information about the issue\n\n_No response_","author":{"url":"https://github.com/mmalerba","@type":"Person","name":"mmalerba"},"datePublished":"2025-03-07T01:46:33Z","url":"https://github.com/microsoft/TypeScript/issues/61369"}
| 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:e1706095-5295-1460-3d63-ae6194c72b82 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E24C:28815B:67C6B8:8C67E9:6B00D737 |
| html-safe-nonce | 3372c9b9b7de82849062f91544dab84fb2d21f50ece44b95eab2c05ecbb40d30 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMjRDOjI4ODE1Qjo2N0M2Qjg6OEM2N0U5OjZCMDBENzM3IiwidmlzaXRvcl9pZCI6Ijg2NjA3MjE4NzgyNDA5MDkxMTEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | c9c7eefd23921a9764b829f41ce9e8547df9c838e05fa26dec53723825506c34 |
| hovercard-subject-tag | issue:2901794678 |
| 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/61369/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f8d93a8320edc724204e71adc295fc7afc52f90d22f93c972bc9fcd11424392e/microsoft/TypeScript/issues/61369 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f8d93a8320edc724204e71adc295fc7afc52f90d22f93c972bc9fcd11424392e/microsoft/TypeScript/issues/61369 |
| og:image:alt | 🔎 Search Terms nullish coalescing parentheses ast 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about nullish coalescing ⏯ Pla... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mmalerba |
| hostname | github.com |
| expected-hostname | github.com |
| None | a481f62a1e04c9b9bdcb5628230682c66d59006f5b13b080f79cba84a176905b |
| 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 | 84c8c5cc25e2fb7f2308428458538436baecc6d2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width