René's URL Explorer Experiment


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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:bd1d668e-c5b5-4987-3cf5-115617e56dfc
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idC692:2F9718:23581D2:2F69662:6B01A20A
html-safe-noncef4c66820b7bbc726f2fe32aea6f6ee302d5405cf8ce9936bacc47f87dba43917
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNjkyOjJGOTcxODoyMzU4MUQyOjJGNjk2NjI6NkIwMUEyMEEiLCJ2aXNpdG9yX2lkIjoiNTkxNjg5NTUzNzQ1MzQ0MTU0NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac6706e3a49d7b5647cfa38b9e11df691acd305fb891b563c959d1f34f8f011186
hovercard-subject-tagissue:3105674241
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/61789/issue_layout
twitter:imagehttps://opengraph.githubassets.com/5bca52b0f787a074cade58e8ea5ea084f3095a0c315e5755820060cde701c886/microsoft/TypeScript/issues/61789
twitter:cardsummary_large_image
og:imagehttps://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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamemkantor
hostnamegithub.com
expected-hostnamegithub.com
Nonecc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c
turbo-cache-controlno-preview
go-importgithub.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git
octolytics-dimension-user_id6154722
octolytics-dimension-user_loginmicrosoft
octolytics-dimension-repository_id20929025
octolytics-dimension-repository_nwomicrosoft/TypeScript
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id20929025
octolytics-dimension-repository_network_root_nwomicrosoft/TypeScript
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release8c77c9e8439031ad54c75a250694842892f693e1
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/microsoft/TypeScript/issues/61789#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F61789
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F61789
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=microsoft%2FTypeScript
Reloadhttps://github.com/microsoft/TypeScript/issues/61789
Reloadhttps://github.com/microsoft/TypeScript/issues/61789
Reloadhttps://github.com/microsoft/TypeScript/issues/61789
microsofthttps://github.com/microsoft
TypeScripthttps://github.com/microsoft/TypeScript
Codehttps://github.com/microsoft/TypeScript
Issues5k (5k)https://github.com/microsoft/TypeScript/issues
Pull requests312 (312)https://github.com/microsoft/TypeScript/pulls
Actionshttps://github.com/microsoft/TypeScript/actions
Projectshttps://github.com/microsoft/TypeScript/projects
Wikihttps://github.com/microsoft/TypeScript/wiki
Security and qualityhttps://github.com/microsoft/TypeScript/security
Insightshttps://github.com/microsoft/TypeScript/insights
https://github.com/microsoft/TypeScript
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/61789
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/61789
Skip assignment narrowing when declaring & initializing a variable in the same statementhttps://github.com/microsoft/TypeScript/issues/61789#top
DeclinedThe issue was declined as something which matches the TypeScript visionhttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Declined%22
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%22
https://github.com/mkantor
Matt Kantor (mkantor)https://github.com/mkantor
on May 31, 2025https://github.com/microsoft/TypeScript/issues/61789#issue-3105674241
https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Typeshttps://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goalshttps://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
assignment narrowinghttps://www.typescriptlang.org/docs/handbook/2/narrowing.html#assignments
why does initialization narrow type? #8513https://github.com/microsoft/TypeScript/issues/8513
Control flow analysis fails to account for possible assignments to captured variables in closures. #30097https://github.com/microsoft/TypeScript/issues/30097
let-union_type is narrowed incorrectly #31580https://github.com/microsoft/TypeScript/issues/31580
Explicit boolean type not honored without cast #34691https://github.com/microsoft/TypeScript/issues/34691
Type narrowing loses explicit type, forces me to re-cast to explicit type #36530https://github.com/microsoft/TypeScript/issues/36530
Is type assignment a simple compatibility check? Or is it an assertion? TypeScript says "both, sometimes" #42656https://github.com/microsoft/TypeScript/issues/42656
"Condition will always return false": declared type not being used? #42702https://github.com/microsoft/TypeScript/issues/42702
Type of variable is narrowed immediately after definition #44824https://github.com/microsoft/TypeScript/issues/44824
Type narrowing not working properly for unions in let declarations #48038https://github.com/microsoft/TypeScript/issues/48038
TypeScript doesn't respect declared function argument type definition #51487https://github.com/microsoft/TypeScript/issues/51487
Type inference ignores explicit type annotation #55330https://github.com/microsoft/TypeScript/issues/55330
Assingment type inference is broken with map, forEach #61755https://github.com/microsoft/TypeScript/issues/61755
#9998https://github.com/microsoft/TypeScript/issues/9998
an identity functionhttps://www.typescriptlang.org/play/?jsx=0#code/MYewdgzgLgBA7gSwCYFMwwLwwDwBUB8AFAB4BcMuAlJvjMQFD0A2KsAhudAE4JgDmMAD4wwAVyZNMI8U3psA-ADooIAKoAHdSi4BhNhBSFqAemMwAZiC4AjZKjCMWsa1MT3s3XgOFiJRX0yU9NZKKhpauvqGJmZsEiBwKEhAA
The "safe upcast" version of satisfieshttps://github.com/microsoft/TypeScript/issues/47920
https://github.com/mkantor
Matt Kantor (mkantor)https://github.com/mkantor
on May 31, 2025https://github.com/microsoft/TypeScript/issues/61789#issuecomment-2925360295
This commenthttps://github.com/microsoft/TypeScript/issues/42702#issuecomment-776073341
https://github.com/mkantor
Matt Kantor (mkantor)https://github.com/mkantor
on May 31, 2025https://github.com/microsoft/TypeScript/issues/61789#issuecomment-2925395937
why does initialization narrow type? #8513https://github.com/microsoft/TypeScript/issues/8513
Type narrowing loses explicit type, forces me to re-cast to explicit type #36530https://github.com/microsoft/TypeScript/issues/36530
Is type assignment a simple compatibility check? Or is it an assertion? TypeScript says "both, sometimes" #42656https://github.com/microsoft/TypeScript/issues/42656
"Condition will always return false": declared type not being used? #42702https://github.com/microsoft/TypeScript/issues/42702
Type inference ignores explicit type annotation #55330https://github.com/microsoft/TypeScript/issues/55330
https://github.com/jcalz
Joe Calzaretta (jcalz)https://github.com/jcalz
on May 31, 2025https://github.com/microsoft/TypeScript/issues/61789#issuecomment-2925534216
https://github.com/mkantor
Matt Kantor (mkantor)https://github.com/mkantor
on May 31, 2025https://github.com/microsoft/TypeScript/issues/61789#issuecomment-2925585442
https://github.com/jcalz
Joe Calzaretta (jcalz)https://github.com/jcalz
on May 31, 2025https://github.com/microsoft/TypeScript/issues/61789#issuecomment-2925684146
https://github.com/mkantor
Matt Kantor (mkantor)https://github.com/mkantor
on May 31, 2025https://github.com/microsoft/TypeScript/issues/61789#issuecomment-2925721546
it can lead to real bugshttps://www.typescriptlang.org/play/#code/MYewdgzgLgBBCmUBiIQBUQDkCuAbXMAvDABQCURAfDAN4wBmqRMYeBAvgFCe6IOoAuOFABOASzABzGAB8WbZgHIARrgCGAC0WcEyVBhz5ynRiAB0UEAFUADjfgiAwmoTkYAencsQMKAE97GAcREBEgA
#9998https://github.com/microsoft/TypeScript/issues/9998
abovehttps://github.com/microsoft/TypeScript/issues/61789#issuecomment-2925360295
https://github.com/jcalz
Joe Calzaretta (jcalz)https://github.com/jcalz
on Jun 1, 2025https://github.com/microsoft/TypeScript/issues/61789#issuecomment-2927364584
#45870https://github.com/microsoft/TypeScript/issues/45870
#49669https://github.com/microsoft/TypeScript/issues/49669
https://github.com/mkantor
Matt Kantor (mkantor)https://github.com/mkantor
on Jun 1, 2025https://github.com/microsoft/TypeScript/issues/61789#issuecomment-2928042450
mentioned in #45870https://github.com/microsoft/TypeScript/issues/45870#issuecomment-919407228
#49669https://github.com/microsoft/TypeScript/issues/49669
https://github.com/RyanCavanaugh
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
on Jun 3, 2025https://github.com/microsoft/TypeScript/issues/61789#issuecomment-2936843089
https://github.com/jcalz
Joe Calzaretta (jcalz)https://github.com/jcalz
on Jun 4, 2025https://github.com/microsoft/TypeScript/issues/61789#issuecomment-2940916252
#49669https://github.com/microsoft/TypeScript/issues/49669
https://github.com/RyanCavanaugh
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
on Jun 4, 2025https://github.com/microsoft/TypeScript/issues/61789#issuecomment-2941843696
https://github.com/jcalz
Joe Calzaretta (jcalz)https://github.com/jcalz
on Jun 4, 2025https://github.com/microsoft/TypeScript/issues/61789#issuecomment-2941849977
https://github.com/mkantor
Matt Kantor (mkantor)https://github.com/mkantor
on Jun 5, 2025https://github.com/microsoft/TypeScript/issues/61789#issuecomment-2945337667
Ryan Cavanaugh (@RyanCavanaugh)https://github.com/RyanCavanaugh
https://github.com/mkantor
Matt Kantor (mkantor)https://github.com/mkantor
on Jun 5, 2025https://github.com/microsoft/TypeScript/issues/61789#issuecomment-2945361039
the assignment narrowing sectionhttps://www.typescriptlang.org/docs/handbook/2/narrowing.html#assignments
the variable annotation sectionhttps://www.typescriptlang.org/docs/handbook/2/everyday-types.html#type-annotations-on-variables
https://github.com/RyanCavanaugh
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
on Jun 5, 2025https://github.com/microsoft/TypeScript/issues/61789#issuecomment-2945971908
#49669https://github.com/microsoft/TypeScript/issues/49669
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%22
DeclinedThe issue was declined as something which matches the TypeScript visionhttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Declined%22
on Jun 9, 2025https://github.com/microsoft/TypeScript/issues/61789#event-18060734348
https://github.com/MartinJohns
Martin Johns (MartinJohns)https://github.com/MartinJohns
on Jun 11, 2025https://github.com/microsoft/TypeScript/issues/61789#issuecomment-2961509294
TyreScrirthttps://github.com/microsoft/TypeScript/issues/31569#issuecomment-495488614
https://github.com/mkantor
Matt Kantor (mkantor)https://github.com/mkantor
on Jun 13, 2025https://github.com/microsoft/TypeScript/issues/61789#issuecomment-2970004342
1https://github.com/microsoft/TypeScript/issues/61789#user-content-fn-1-edf4f1d7a031f2d2ac053ae961b6b1ae
Ryan Cavanaugh (@RyanCavanaugh)https://github.com/RyanCavanaugh
experimenthttps://github.com/microsoft/TypeScript/pull/61818
↩https://github.com/microsoft/TypeScript/issues/61789#user-content-fnref-1-edf4f1d7a031f2d2ac053ae961b6b1ae
https://github.com/typescript-bot
TypeScript Bot (typescript-bot)https://github.com/typescript-bot
on Jun 16, 2025https://github.com/microsoft/TypeScript/issues/61789#issuecomment-2974865189
TypeScript Bot (typescript-bot)https://github.com/typescript-bot
not plannedhttps://github.com/microsoft/TypeScript/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Anot-planned
on Jun 16, 2025https://github.com/microsoft/TypeScript/issues/61789#event-18160375647
Sign up for freehttps://github.com/signup?return_to=https://github.com/microsoft/TypeScript/issues/61789
Sign in to commenthttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/61789
DeclinedThe issue was declined as something which matches the TypeScript visionhttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Declined%22
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.