Title: Skip assignment narrowing when declaring & initializing a variable in the same statement · Issue #61789 · microsoft/TypeScript · GitHub
Open Graph Title: Skip assignment narrowing when declaring & initializing a variable in the same statement · Issue #61789 · microsoft/TypeScript
X Title: Skip assignment narrowing when declaring & initializing a variable in the same statement · Issue #61789 · microsoft/TypeScript
Description: 🔍 Search Terms declare, assign, declaration, assignment, narrow, narrowing, union, variable, "control flow analysis" ✅ Viability Checklist This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn't change the...
Open Graph Description: 🔍 Search Terms declare, assign, declaration, assignment, narrow, narrowing, union, variable, "control flow analysis" ✅ Viability Checklist This wouldn't be a breaking change in existing TypeScript/...
X Description: 🔍 Search Terms declare, assign, declaration, assignment, narrow, narrowing, union, variable, "control flow analysis" ✅ Viability Checklist This wouldn't be a breaking change in existi...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/61789
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Skip assignment narrowing when declaring & initializing a variable in the same statement","articleBody":"### 🔍 Search Terms\n\ndeclare, assign, declaration, assignment, narrow, narrowing, union, variable, \"control flow analysis\"\n\n### ✅ Viability Checklist\n\n- [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code\n- [x] This wouldn't change the runtime behavior of existing JavaScript code\n- [x] This could be implemented without emitting different JS based on the types of the expressions\n- [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)\n- [x] This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types\n- [x] This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals\n\n### ⭐ Suggestion\n\nCurrently TypeScript performs [assignment narrowing](https://www.typescriptlang.org/docs/handbook/2/narrowing.html#assignments) whenever a variable with a union type is assigned to.\n\nI propose that assignment narrowing should _not_ occur in the specific case where a variable is declared and initialized in a single statement with a type annotation (e.g. immediately following `let a: B = c`, `a` would be guaranteed to have exactly type `B`). This behavior may need to live behind a compiler flag to avoid problems in existing code which assumes such narrowing happens. If the current narrowing behavior is desired at some particular assignment, it could be done in a separate statement (`let a: B; a = c`).\n\nThere have been many issues filed related to assignment narrowing, but I haven't seen this specific idea suggested as a \"feature request\" anywhere. Apologies if I missed a duplicate.\n\n### 📃 Motivating Example\n\nThere are tons of existing issues with relevant examples:\n- #8513\n- #30097\n- #31580\n- #34691\n- #36530\n- #42656\n- #42702\n- #44824\n- #48038\n- #51487\n- #55330\n- #61755\n- …probably many I missed\n\nWhile some of the above touch on #9998 and related CFA limitations, as far as I can tell the specific examples in those issues would all cease being problematic if this suggestion were implemented.\n\nI think type inference has trained me (and perhaps others) to take a mental shortcut: it's easy to conceptualize most assignments as if the type checker examined the right-hand side _first_ (before dealing with the left), in which case it feels like the `: B` should \"win\" in `let a: B = c` and act as an upcast (because `: B` would be applied _after_ determining the type of `c`). I know that's not literally how things work, but the actual behavior is harder to explain and keep front-of-mind at all times. This is made worse by the fact that when `B` is a non-union type then it does act like an upcast (e.g. `a` is `unknown` rather than `string` after `let a: unknown = 'blah'`).\n\n### 💻 Use Cases\n\n1. What do you want to use this for?\n Declaring a union-typed variable and giving it an initial value without having to work around the type checker.\n\n2. What shortcomings exist with current approaches?\n As far as I can tell the suggested approach is usually a type assertion (instead of `let a: B = c`, write `let a = c as B`). Shortcomings of that approach include:\n - it's not always obvious when it will be necessary, especially when the union is hiding behind a type alias\n - it's easy to accidentally assert the wrong type (or change the type/value in a future refactor) without getting any feedback, leading to runtime bugs that could have easily been caught by the type checker\n - if you have a lint rule banning type assertions it needs to be disabled\n\n3. What workarounds are you using in the meantime?\n Type assertions as mentioned above, or [an identity function](https://www.typescriptlang.org/play/?jsx=0#code/MYewdgzgLgBA7gSwCYFMwwLwwDwBUB8AFAB4BcMuAlJvjMQFD0A2KsAhudAE4JgDmMAD4wwAVyZNMI8U3psA-ADooIAKoAHdSi4BhNhBSFqAemMwAZiC4AjZKjCMWsa1MT3s3XgOFiJRX0yU9NZKKhpauvqGJmZsEiBwKEhAA).\n\n [The \"safe upcast\" version of `satisfies`](https://github.com/microsoft/TypeScript/issues/47920) would have provided another workaround, but that's not the version that shipped.","author":{"url":"https://github.com/mkantor","@type":"Person","name":"mkantor"},"datePublished":"2025-05-31T15:52:57Z","url":"https://github.com/microsoft/TypeScript/issues/61789"}
| 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:bd1d668e-c5b5-4987-3cf5-115617e56dfc |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C692:2F9718:23581D2:2F69662:6B01A20A |
| html-safe-nonce | f4c66820b7bbc726f2fe32aea6f6ee302d5405cf8ce9936bacc47f87dba43917 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNjkyOjJGOTcxODoyMzU4MUQyOjJGNjk2NjI6NkIwMUEyMEEiLCJ2aXNpdG9yX2lkIjoiNTkxNjg5NTUzNzQ1MzQ0MTU0NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 6706e3a49d7b5647cfa38b9e11df691acd305fb891b563c959d1f34f8f011186 |
| hovercard-subject-tag | issue:3105674241 |
| 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/61789/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5bca52b0f787a074cade58e8ea5ea084f3095a0c315e5755820060cde701c886/microsoft/TypeScript/issues/61789 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5bca52b0f787a074cade58e8ea5ea084f3095a0c315e5755820060cde701c886/microsoft/TypeScript/issues/61789 |
| og:image:alt | 🔍 Search Terms declare, assign, declaration, assignment, narrow, narrowing, union, variable, "control flow analysis" ✅ Viability Checklist This wouldn't be a breaking change in existing TypeScript/... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mkantor |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| 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 | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width