Title: instanceof type guard missing deduction · Issue #20138 · microsoft/TypeScript · GitHub
Open Graph Title: instanceof type guard missing deduction · Issue #20138 · microsoft/TypeScript
X Title: instanceof type guard missing deduction · Issue #20138 · microsoft/TypeScript
Description: TypeScript Version: 2.7.0-dev.201xxxxx Code class C { } function f
Open Graph Description: TypeScript Version: 2.7.0-dev.201xxxxx Code class C { } function f
X Description: TypeScript Version: 2.7.0-dev.201xxxxx Code class C { } function f<T extends C>(v: T | string): void { if (v instanceof C) { const x: T = v; console.log(x); } } Expected behavior: No compile ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/20138
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"instanceof type guard missing deduction","articleBody":"\u003c!-- BUGS: Please use this template. -->\r\n\u003c!-- QUESTIONS: This is not a general support forum! Ask Qs at http://stackoverflow.com/questions/tagged/typescript -->\r\n\u003c!-- SUGGESTIONS: See https://github.com/Microsoft/TypeScript-wiki/blob/master/Writing-Good-Design-Proposals.md -->\r\n\r\n\u003c!-- Please try to reproduce the issue with `typescript@next`. It may have already been fixed. -->\r\n**TypeScript Version:** 2.7.0-dev.201xxxxx\r\n\r\n**Code**\r\n\r\n```ts\r\nclass C { }\r\n\r\nfunction f\u003cT extends C>(v: T | string): void {\r\n if (v instanceof C) {\r\n const x: T = v;\r\n console.log(x);\r\n }\r\n}\r\n\r\n```\r\n\r\n**Expected behavior:**\r\n\r\nNo compile errors:\r\n\r\n**Actual behavior:**\r\n\r\np.ts(5,11): error TS2322: Type 'string | T' is not assignable to type 'T'.\r\n Type 'string' is not assignable to type 'T'.\r\n","author":{"url":"https://github.com/abramobagnara","@type":"Person","name":"abramobagnara"},"datePublished":"2017-11-19T12:50:34Z","url":"https://github.com/microsoft/TypeScript/issues/20138"}
| 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:57eacb31-429e-1abb-e82c-d6bd5e053007 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9D06:10D9F8:AC4228:E53030:6B00E9D8 |
| html-safe-nonce | 97797217327850a25604cdd1a9e0a453cf0be05413d1c9e384c2dcbcb1882fc9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5RDA2OjEwRDlGODpBQzQyMjg6RTUzMDMwOjZCMDBFOUQ4IiwidmlzaXRvcl9pZCI6IjE0NTE1OTU5MDI5ODc4NTYzNDQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | f75b577189dd376ebfe1f547d664aee7456d767dfc1393a2c2f1d64c507ca217 |
| hovercard-subject-tag | issue:275156249 |
| 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/20138/issue_layout |
| twitter:image | https://opengraph.githubassets.com/6a023f1442faa803f0b39e3c05ab0f1641c4d13439ac80b538064349cb92f099/microsoft/TypeScript/issues/20138 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/6a023f1442faa803f0b39e3c05ab0f1641c4d13439ac80b538064349cb92f099/microsoft/TypeScript/issues/20138 |
| og:image:alt | TypeScript Version: 2.7.0-dev.201xxxxx Code class C { } function f |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | abramobagnara |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4ef498cbabcbe3c50befc7a5b743e7072ee770b4daf0e15e54801180d1fa91fc |
| 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 | 9983dd5da06943f833c971dd5b6bc988e2858be5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width