Title: Type narrowing not working properly for unions in let declarations · Issue #48038 · microsoft/TypeScript · GitHub
Open Graph Title: Type narrowing not working properly for unions in let declarations · Issue #48038 · microsoft/TypeScript
X Title: Type narrowing not working properly for unions in let declarations · Issue #48038 · microsoft/TypeScript
Description: Bug Report When you type a variable with a union type, typescript is too aggressive while narrowing it down. Specifically, it only considers assignments in the "main" flow, and ignores any other possibility. 🔎 Search Terms let, type guar...
Open Graph Description: Bug Report When you type a variable with a union type, typescript is too aggressive while narrowing it down. Specifically, it only considers assignments in the "main" flow, and ignores any other po...
X Description: Bug Report When you type a variable with a union type, typescript is too aggressive while narrowing it down. Specifically, it only considers assignments in the "main" flow, and ignores an...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/48038
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type narrowing not working properly for unions in let declarations","articleBody":"# Bug Report\r\n\r\nWhen you type a variable with a union type, typescript is too aggressive while narrowing it down. Specifically, it only considers assignments in the \"main\" flow, and ignores any other possibility.\r\n\r\n### 🔎 Search Terms\r\n\r\nlet, type guard, type narrowing, never\r\n\r\n### 🕗 Version & Regression Information\r\n\r\nI've tried with different TS versions starting in `3.`, and it happens in all of them. I also tried with nightly (v4.7.0-dev.20220215)\r\n\r\n### ⏯ Playground Link\r\n\r\n[Playground link with relevant code](https://www.typescriptlang.org/play?ts=4.5.4#code/DYUwLgBAZg9jBcEDOYBOBLAdgcwgHwkwFcBbAIxFQgF4IAWAJgG4BYAKCiMwGMx0ZMEMgENUACgCUEAN7sI0ODQgByRstZsAvu3YjxEjQHpDCmEtUN1EYxHRQIAdxAQu3GCRIhMkMAAt0SAA0jiDKqM5QWCDsdhBiYACeAA4gMPawZtRZKigYOMpSsmzyGQB0WNzARAAmIEhiygAeBUzWJgAKqDApqIkqFVW1SMoQ1TB1hDCQII0BkAIQiSkqmCAAbpTKpezabEA)\r\n\r\n### 💻 Code\r\n\r\n```typescript\r\nlet foo: string | number = 42;\r\nfunction bar() {\r\n foo = '42';\r\n}\r\n\r\nbar();\r\n// foo = '42'; // if we uncomment this, we're fine\r\nif (typeof foo === 'string') {\r\n foo.includes('x'); // TS Error: property 'includes' does not exist on type 'never'\r\n}\r\n```\r\n\r\n### 🙁 Actual behavior\r\n\r\nTS shouldn't be so eager to narrow the type to `never`. \r\n\r\n### 🙂 Expected behavior\r\n\r\nDon't narrow unions based on assignments. Or, if it's not too hard to implement, do it only when you can be absolutely certain. I.e., no function after the assignment that caused the type narrowing.\r\n","author":{"url":"https://github.com/lgenzelis","@type":"Person","name":"lgenzelis"},"datePublished":"2022-02-25T13:59:37Z","url":"https://github.com/microsoft/TypeScript/issues/48038"}
| 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:4d58ebc8-b6bf-22fb-eb64-ee4986877b9d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B24C:14700E:2B1FF16:36C26F5:6B01AB1D |
| html-safe-nonce | e5b71bf19a7403103062b3bd40c2c1c723d0184cea2b88a18e4c565970914928 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMjRDOjE0NzAwRToyQjFGRjE2OjM2QzI2RjU6NkIwMUFCMUQiLCJ2aXNpdG9yX2lkIjoiNzU4MDY1MjQ3NjQ5MjI2MjE3MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | b655278a53e310484195d66c808f960dc5b09c5273447564e93aee9fdf2d321d |
| hovercard-subject-tag | issue:1150477181 |
| 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/48038/issue_layout |
| twitter:image | https://opengraph.githubassets.com/fec4aa086d3904e2d04ace2d340b9e18bfe7dd1c649b339202b997d8a914d72f/microsoft/TypeScript/issues/48038 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/fec4aa086d3904e2d04ace2d340b9e18bfe7dd1c649b339202b997d8a914d72f/microsoft/TypeScript/issues/48038 |
| og:image:alt | Bug Report When you type a variable with a union type, typescript is too aggressive while narrowing it down. Specifically, it only considers assignments in the "main" flow, and ignores any other po... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | lgenzelis |
| 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