Title: instanceof type inference broken in an "if -> else if -> else if" chain · Issue #11965 · microsoft/TypeScript · GitHub
Open Graph Title: instanceof type inference broken in an "if -> else if -> else if" chain · Issue #11965 · microsoft/TypeScript
X Title: instanceof type inference broken in an "if -> else if -> else if" chain · Issue #11965 · microsoft/TypeScript
Description: TypeScript Version: "typescript@2.0.6" // A *self-contained* demonstration of the problem follows... class Action1 { public constructor(public propA: string) {} } class ActionX { } class Action2 { public constructor(public propB: string)...
Open Graph Description: TypeScript Version: "typescript@2.0.6" // A *self-contained* demonstration of the problem follows... class Action1 { public constructor(public propA: string) {} } class ActionX { } class Action2 { ...
X Description: TypeScript Version: "typescript@2.0.6" // A *self-contained* demonstration of the problem follows... class Action1 { public constructor(public propA: string) {} } class ActionX { } class ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/11965
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"instanceof type inference broken in an \"if -> else if -> else if\" chain","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**TypeScript Version:** \"typescript@2.0.6\"\r\n\r\n```ts\r\n// A *self-contained* demonstration of the problem follows...\r\nclass Action1 {\r\n public constructor(public propA: string) {}\r\n}\r\n\r\nclass ActionX {\r\n\r\n}\r\n\r\nclass Action2 {\r\n public constructor(public propB: string) {}\r\n}\r\n\r\nclass FooBar {\r\n\r\n public handleAction(action: Object): void {\r\n if (action instanceof Action1) {\r\n console.log(action.propA); // works\r\n } else if (action instanceof ActionX) {\r\n // The presense of this empty clause is essential\r\n } else if (action instanceof Action2) {\r\n console.log(action.propB); // throws compile error: Error TS2339: Property 'propB' does not exist on type 'never'.\r\n }\r\n }\r\n}\r\n```\r\n\r\n**Expected behavior:**\r\nThe code to compile without an error.\r\n**Actual behavior:** \r\nThe code above throws a compile error:\r\nError TS2339: Property 'propB' does not exist on type 'never'.\r\n\r\nA **workaround** for the issue is to use an explicit cast for the last \"else if\" clause: \r\n\r\n```ts\r\n } else if (action instanceof Action2) {\r\n const a = action as Action2;\r\n console.log(a.propB); // now it works\r\n }\r\n```\r\n","author":{"url":"https://github.com/stkomarov","@type":"Person","name":"stkomarov"},"datePublished":"2016-10-31T19:47:52Z","url":"https://github.com/microsoft/TypeScript/issues/11965"}
| 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:3781a5d1-495e-908f-0ee5-56e00f9131a2 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8ED2:213839:FC9E68:1458BA1:6B0101E3 |
| html-safe-nonce | 395dff0224afa901772575a42aa62239c8db7da30f8001d8f91d0f85f5438a80 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RUQyOjIxMzgzOTpGQzlFNjg6MTQ1OEJBMTo2QjAxMDFFMyIsInZpc2l0b3JfaWQiOiI0MDU5OTY5MTU0MzMyMDMzNTA3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 0bd85466709eaa9849d02bc6441f3708665d74b2fd1916a7a349c5d5a945e3f6 |
| hovercard-subject-tag | issue:186379347 |
| 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/11965/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b23026880fa08014d52d38dadf38c62796fc1d82d7002cfde97446729ce7d1f1/microsoft/TypeScript/issues/11965 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b23026880fa08014d52d38dadf38c62796fc1d82d7002cfde97446729ce7d1f1/microsoft/TypeScript/issues/11965 |
| og:image:alt | TypeScript Version: "typescript@2.0.6" // A *self-contained* demonstration of the problem follows... class Action1 { public constructor(public propA: string) {} } class ActionX { } class Action2 { ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | stkomarov |
| hostname | github.com |
| expected-hostname | github.com |
| None | cb7fa109eca7d4156c889f474306b0e91336055e9adf8fd4684b61e49094d124 |
| 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 | 2b59d31a4e44f08b7af67a4b67f8fd0af3befe2a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width