Title: instanceof typeguard fails with derived classes that have unsound overrides · Issue #17612 · microsoft/TypeScript · GitHub
Open Graph Title: instanceof typeguard fails with derived classes that have unsound overrides · Issue #17612 · microsoft/TypeScript
X Title: instanceof typeguard fails with derived classes that have unsound overrides · Issue #17612 · microsoft/TypeScript
Description: TypeScript Version: 2.3.4 and 2.4.1 Code interface IBaseModel { id: string } class BaseClass { model: IBaseModel constructor() { } setModel(model: IBaseModel) { this.model = model } getValueByName(name: string) { return this.model[name];...
Open Graph Description: TypeScript Version: 2.3.4 and 2.4.1 Code interface IBaseModel { id: string } class BaseClass { model: IBaseModel constructor() { } setModel(model: IBaseModel) { this.model = model } getValueByName(...
X Description: TypeScript Version: 2.3.4 and 2.4.1 Code interface IBaseModel { id: string } class BaseClass { model: IBaseModel constructor() { } setModel(model: IBaseModel) { this.model = model } getValueByName(...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/17612
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"instanceof typeguard fails with derived classes that have unsound overrides","articleBody":"**TypeScript Version:** \r\n\r\n2.3.4 and 2.4.1\r\n\r\n**Code**\r\n\r\n```ts\r\ninterface IBaseModel {\r\n id: string\r\n}\r\n\r\nclass BaseClass {\r\n model: IBaseModel\r\n constructor() {\r\n }\r\n\r\n setModel(model: IBaseModel) {\r\n this.model = model\r\n }\r\n\r\n getValueByName(name: string) {\r\n return this.model[name];\r\n }\r\n}\r\n\r\ninterface IDerived1Model extends IBaseModel {\r\n height: number;\r\n}\r\n\r\nclass Derived1 extends BaseClass {\r\n // Unsound override!\r\n setModel(model: IDerived1Model) {\r\n super.setModel(model);\r\n // Do something with model...\r\n }\r\n}\r\n\r\ninterface IDerived2Model extends IBaseModel {\r\n width: number;\r\n}\r\n\r\nclass Derived2 extends BaseClass {\r\n // Unsound override!\r\n setModel(model: IDerived2Model) {\r\n super.setModel(model);\r\n // Do something with model...\r\n }\r\n}\r\n\r\nconst model1 = { id: \"0\", height: 42 };\r\nconst model2 = { id: \"1\", width: 24 };\r\n\r\nconst obj1 = new Derived1();\r\nobj1.setModel(model1);\r\n\r\nconst obj2 = new Derived2();\r\nobj2.setModel(model2);\r\n\r\nconst objs: BaseClass[] = [\r\n obj1,\r\n obj2\r\n];\r\n\r\nlet variable: any = null;\r\nfor (const obj of objs) {\r\n if (obj instanceof Derived1) {\r\n variable = obj.getValueByName(\"height\"); // Ok, obj is now of type `Derived1`\r\n } else if (obj instanceof Derived2) {\r\n variable = obj.getValueByName(\"width\"); // Does not compile: Property 'getValueByName' does not exist on type 'never'\r\n }\r\n console.log(\"Value is: \" + variable);\r\n}\r\n```\r\n\r\n**Expected behavior:**\r\n`obj` is narrowed to `Derived2`.\r\n\r\n**Actual behavior:**\r\n`obj` is narrowed to `never`.\r\n\r\n## More\r\nSee discussion here: https://stackoverflow.com/questions/45381122/typescript-type-narrowed-to-never-with-instanceof-in-an-if-else-statement\r\nWhen the `setModel` overrides use `IBaseModel` as type, it works. However, even though the current code is unsound, I would expect it to compile.","author":{"url":"https://github.com/SebGrenier","@type":"Person","name":"SebGrenier"},"datePublished":"2017-08-04T12:39:59Z","url":"https://github.com/microsoft/TypeScript/issues/17612"}
| 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:463f8326-9b31-522e-8a68-c60f7cd28d6a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CEB2:DB09F:BEE6A7:F4B60B:6B00FED9 |
| html-safe-nonce | 45ec4a75ed19143885439226d2a897bd6129040531cb1b4d15e4af4782dab66f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRUIyOkRCMDlGOkJFRTZBNzpGNEI2MEI6NkIwMEZFRDkiLCJ2aXNpdG9yX2lkIjoiNDQyNTk3ODM3NjQxOTQwNjk3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | f337f05be56eb8233920d7a493da2d749bcee10d2050015b519e3c245ae00cba |
| hovercard-subject-tag | issue:247994152 |
| 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/17612/issue_layout |
| twitter:image | https://opengraph.githubassets.com/0361a46203ceb35b6597c606c887b30600e768b5bebc8712cd88c50127dc16e9/microsoft/TypeScript/issues/17612 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/0361a46203ceb35b6597c606c887b30600e768b5bebc8712cd88c50127dc16e9/microsoft/TypeScript/issues/17612 |
| og:image:alt | TypeScript Version: 2.3.4 and 2.4.1 Code interface IBaseModel { id: string } class BaseClass { model: IBaseModel constructor() { } setModel(model: IBaseModel) { this.model = model } getValueByName(... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | SebGrenier |
| 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