Title: Type narrowing loses explicit type, forces me to re-cast to explicit type · Issue #36530 · microsoft/TypeScript · GitHub
Open Graph Title: Type narrowing loses explicit type, forces me to re-cast to explicit type · Issue #36530 · microsoft/TypeScript
X Title: Type narrowing loses explicit type, forces me to re-cast to explicit type · Issue #36530 · microsoft/TypeScript
Description: TypeScript Version: 3.8-Beta Search Terms: Type narrowing explicit type lost concat Sorry if this is a duplicate – I failed to find any related issues. Code type A = 1 | 2; const one: A = 1; [one].concat([2]); // type error [one as A].co...
Open Graph Description: TypeScript Version: 3.8-Beta Search Terms: Type narrowing explicit type lost concat Sorry if this is a duplicate – I failed to find any related issues. Code type A = 1 | 2; const one: A = 1; [one]....
X Description: TypeScript Version: 3.8-Beta Search Terms: Type narrowing explicit type lost concat Sorry if this is a duplicate – I failed to find any related issues. Code type A = 1 | 2; const one: A = 1; [one]....
Opengraph URL: https://github.com/microsoft/TypeScript/issues/36530
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type narrowing loses explicit type, forces me to re-cast to explicit type","articleBody":"\u003c!-- 🚨 STOP 🚨 𝗦𝗧𝗢𝗣 🚨 𝑺𝑻𝑶𝑷 🚨 -->\r\n\u003c!--\r\nHalf of all issues filed here are duplicates, answered in the FAQ, or not appropriate for the bug tracker.\r\n\r\nPlease help us by doing the following steps before logging an issue:\r\n * Search: https://github.com/Microsoft/TypeScript/search?type=Issues\r\n * Read the CONTRIBUTING guidelines: https://github.com/Microsoft/TypeScript/blob/master/CONTRIBUTING.md\r\n * Read the FAQ: https://github.com/Microsoft/TypeScript/wiki/FAQ\r\n-->\r\n\r\n\u003c!-- If you have a QUESTION:\r\n THIS IS NOT A FORUM FOR QUESTIONS.\r\n Ask questions at http://stackoverflow.com/questions/tagged/typescript\r\n or https://gitter.im/Microsoft/TypeScript\r\n-->\r\n\r\n\u003c!-- If you have a SUGGESTION:\r\n Most suggestion reports are duplicates, please search extra hard before logging a new suggestion.\r\n See https://github.com/Microsoft/TypeScript-wiki/blob/master/Writing-Good-Design-Proposals.md\r\n-->\r\n\r\n\u003c!-- If you have a BUG:\r\n Please fill in the *entire* template below.\r\n-->\r\n\r\n\u003c!-- Please try to reproduce the issue with `typescript@next`. It may have already been fixed. -->\r\n**TypeScript Version:** 3.8-Beta\r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms:** Type narrowing explicit type lost concat\r\n\r\nSorry if this is a duplicate – I failed to find any related issues.\r\n\r\n**Code**\r\n\r\n```ts\r\ntype A = 1 | 2;\r\n\r\nconst one: A = 1;\r\n\r\n[one].concat([2]); // type error\r\n[one as A].concat([2]); // types alright\r\n```\r\n\r\n**Expected behavior:** TypeScript's type narrowing is super useful, but here, I stumbled about a case where it introduces a type error, forcing me to manually re-assert my original type. I'd expect that the program above is typed correctly…\r\n\r\n**Actual behavior:** …but TypeScript infers (correctly) that `one` has type `1`, which is more specific than `A`. Unfortunately, this means I no longer can `concat` something of type `A[]` (or `[2]`) to it, I have to explicitly re-cast `one` to its original type.\r\n\r\nI'm not sure whether this qualifies as a bug or a suggestion – either way, it's sad, and I'd like for this to change.\r\n\r\n**Playground Link:** https://www.typescriptlang.org/play/index.html?ts=3.8-Beta#code/C4TwDgpgBAglC8UCMUA+UBMBuAUDgxgPYB2AzsFCRAFywLK44DaVAugHRHH4CGwAFEwysAlFigB6CVFCQoEAE4LCC5lSg9SsDl14Cho8VJngIWngBsFASwDmAC2A4gA\r\n\r\n**Related Issues:** Couldn't find any 🤔\r\n","author":{"url":"https://github.com/fefrei","@type":"Person","name":"fefrei"},"datePublished":"2020-01-30T11:30:54Z","url":"https://github.com/microsoft/TypeScript/issues/36530"}
| 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:029be1a4-f328-da69-f613-40acd55ac0c9 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C104:65478:29240AF:3699649:6B01AFE4 |
| html-safe-nonce | b4c5587775e20e1cd172a9f004cf57f75a269c86aa0417687d6c0bfea562802b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMTA0OjY1NDc4OjI5MjQwQUY6MzY5OTY0OTo2QjAxQUZFNCIsInZpc2l0b3JfaWQiOiI1ODkzNzQyNTc5OTQxNDg2NTY0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 544d4aac5c4268d80c6fd2f1fe6e3a77681297f4f858df54adc9e55af8a03d24 |
| hovercard-subject-tag | issue:557433566 |
| 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/36530/issue_layout |
| twitter:image | https://opengraph.githubassets.com/414c6dbcd163dcf7767ca66bb895e5642bc5c422b7acd132ba7707787fa8d8ec/microsoft/TypeScript/issues/36530 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/414c6dbcd163dcf7767ca66bb895e5642bc5c422b7acd132ba7707787fa8d8ec/microsoft/TypeScript/issues/36530 |
| og:image:alt | TypeScript Version: 3.8-Beta Search Terms: Type narrowing explicit type lost concat Sorry if this is a duplicate – I failed to find any related issues. Code type A = 1 | 2; const one: A = 1; [one].... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | fefrei |
| 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