Title: "Condition will always return false": declared type not being used? · Issue #42702 · microsoft/TypeScript · GitHub
Open Graph Title: "Condition will always return false": declared type not being used? · Issue #42702 · microsoft/TypeScript
X Title: "Condition will always return false": declared type not being used? · Issue #42702 · microsoft/TypeScript
Description: Suggestion 🔍 Search Terms union condition always return false inferred ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaScript code [I think it would only make more...
Open Graph Description: Suggestion 🔍 Search Terms union condition always return false inferred ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaScri...
X Description: Suggestion 🔍 Search Terms union condition always return false inferred ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/Java...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/42702
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"\"Condition will always return false\": declared type not being used?","articleBody":"# Suggestion\r\n\r\n## 🔍 Search Terms\r\n\r\nunion condition always return false inferred\r\n\r\n## ✅ Viability Checklist\r\n\r\nMy suggestion meets these guidelines:\r\n\r\n* [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code [I think it would only make more code accepted, but I'm not sure.]\r\n* [x] This wouldn't change the runtime behavior of existing JavaScript code\r\n* [x] This could be implemented without emitting different JS based on the types of the expressions\r\n* [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)\r\n* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).\r\n\r\n\r\n## ⭐ Suggestion\r\n\r\n[Playground link](https://www.typescriptlang.org/play?ssl=4&ssc=2&pln=1&pc=1#code/MYewdgzgLgBAHgLhgcgIbJgHxQIwwXhXQG4AoASwDMYAKOGfR3ZAShgG8YB6LmAFQDKAJgDMANgDsSPgAtyEGKDAATclHLgYAd3IAbXTFS6tqAJ4KATgFMoAVwtgUlIxCsYI5MMCswoMn1CmAA5WCsgARKjhGKgqKOE40TAyqABuPmAgMCDpFrqoQQB0pDCliuAQILpWhbogAOY0yHgsZAC+QA)\r\n\r\n```typescript\r\nconst x: 'a' | 'b' = 'a';\r\nif (x === 'b') { // TS2367: This condition will always return 'false' since the types '\"a\"' and '\"b\"' have no overlap.\r\n console.log('b');\r\n}\r\n```\r\n\r\nSince I've declared `x` to have type `'a' | 'b'`, I was surprised that TypeScript didn't respect my declared type. But maybe TS does this a lot and I just haven't noticed?\r\n\r\nI think this is a useful error to report, but it feels like something you only want when you're ready to commit, like the \"unused variable\" warning. When I'm debugging/experimenting and moving a code around, I don't want this to prevent compilation.\r\n\r\n## 📃 Motivating Example\r\n\r\nSee above.\r\n\r\n## 💻 Use Cases\r\n\r\nSee above.\r\n","author":{"url":"https://github.com/cakoose","@type":"Person","name":"cakoose"},"datePublished":"2021-02-09T01:14:29Z","url":"https://github.com/microsoft/TypeScript/issues/42702"}
| 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:7d018d37-0320-3dd3-297f-34f16e371ac8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9C9C:23CF06:30AEB7B:3E06D36:6B01BBB9 |
| html-safe-nonce | 2a08cebf4ef9ffb9e11534736f46c0909ae4f92333609a3746fff305acdec910 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QzlDOjIzQ0YwNjozMEFFQjdCOjNFMDZEMzY6NkIwMUJCQjkiLCJ2aXNpdG9yX2lkIjoiMTEyODQwMjQ3MzM4OTc0MTM3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 688c0821dc3d2d8a2acb141c07dd8bb5d2e31193fdfc340923f516412f6d640f |
| hovercard-subject-tag | issue:804103469 |
| 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/42702/issue_layout |
| twitter:image | https://opengraph.githubassets.com/fa00ec2a874e072a82c71bd901ac87288e7eb38a75ea43229d71022f9887907d/microsoft/TypeScript/issues/42702 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/fa00ec2a874e072a82c71bd901ac87288e7eb38a75ea43229d71022f9887907d/microsoft/TypeScript/issues/42702 |
| og:image:alt | Suggestion 🔍 Search Terms union condition always return false inferred ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaScri... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | cakoose |
| 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