Title: Type narrowing with foo.constructor · Issue #16035 · microsoft/TypeScript · GitHub
Open Graph Title: Type narrowing with foo.constructor · Issue #16035 · microsoft/TypeScript
X Title: Type narrowing with foo.constructor · Issue #16035 · microsoft/TypeScript
Description: TypeScript Version: 2.3.1 Code class FooTask { foo: number; } class BarTask { bar: number; } type taskType = FooTask|BarTask; function createEvent(task: taskType) { if (task instanceof FooTask) { task.foo; // Works. } switch (task.constr...
Open Graph Description: TypeScript Version: 2.3.1 Code class FooTask { foo: number; } class BarTask { bar: number; } type taskType = FooTask|BarTask; function createEvent(task: taskType) { if (task instanceof FooTask) { t...
X Description: TypeScript Version: 2.3.1 Code class FooTask { foo: number; } class BarTask { bar: number; } type taskType = FooTask|BarTask; function createEvent(task: taskType) { if (task instanceof FooTask) { t...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/16035
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type narrowing with foo.constructor","articleBody":"**TypeScript Version:** 2.3.1\r\n\r\n**Code**\r\n\r\n```ts\r\nclass FooTask {\r\n foo: number;\r\n}\r\n\r\nclass BarTask {\r\n bar: number;\r\n}\r\n\r\ntype taskType = FooTask|BarTask;\r\n\r\nfunction createEvent(task: taskType) {\r\n if (task instanceof FooTask) {\r\n task.foo; // Works.\r\n }\r\n\r\n switch (task.constructor) {\r\n case FooTask:\r\n task.foo; // Fails.\r\n }\r\n}\r\n```\r\n\r\nIt would be nice if the second worked like the first; narrowed the type of `task` appropriately.","author":{"url":"https://github.com/calebegg","@type":"Person","name":"calebegg"},"datePublished":"2017-05-23T18:19:36Z","url":"https://github.com/microsoft/TypeScript/issues/16035"}
| 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:415ddeea-4e37-f19a-33dc-e8f0a20a1a9f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 82B0:38C5CF:D91538:117323E:6B00F19C |
| html-safe-nonce | 1dbadc7e2790407578cc15b4aa731a0ff6b3b06f4d61cff22f8519d5506fe9fb |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MkIwOjM4QzVDRjpEOTE1Mzg6MTE3MzIzRTo2QjAwRjE5QyIsInZpc2l0b3JfaWQiOiIxMDkyNjU5ODQxNDUxMjU3ODgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | de803433bdef5b2af58b6a0c52c7c264362d675f7d25967535b1c62a7f5e179e |
| hovercard-subject-tag | issue:230799628 |
| 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/16035/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1ed69dc1d7867401f265ecb3cf9c473f697cac22733add8fa647c431eeaaf927/microsoft/TypeScript/issues/16035 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1ed69dc1d7867401f265ecb3cf9c473f697cac22733add8fa647c431eeaaf927/microsoft/TypeScript/issues/16035 |
| og:image:alt | TypeScript Version: 2.3.1 Code class FooTask { foo: number; } class BarTask { bar: number; } type taskType = FooTask|BarTask; function createEvent(task: taskType) { if (task instanceof FooTask) { t... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | calebegg |
| 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