Title: Use `Symbol.hasInstance` for `instanceof` narrowing, include constructor as part of class structural type · Issue #17360 · microsoft/TypeScript · GitHub
Open Graph Title: Use `Symbol.hasInstance` for `instanceof` narrowing, include constructor as part of class structural type · Issue #17360 · microsoft/TypeScript
X Title: Use `Symbol.hasInstance` for `instanceof` narrowing, include constructor as part of class structural type · Issue #17360 · microsoft/TypeScript
Description: My proposal: For instanceof expressions, if Symbol.hasInstance exists on the RHS, narrow the expression according to the method's return value. For classes, implicitly add a type-level constructor prototype property which is typeof class...
Open Graph Description: My proposal: For instanceof expressions, if Symbol.hasInstance exists on the RHS, narrow the expression according to the method's return value. For classes, implicitly add a type-level constructor ...
X Description: My proposal: For instanceof expressions, if Symbol.hasInstance exists on the RHS, narrow the expression according to the method's return value. For classes, implicitly add a type-level construc...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/17360
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Use `Symbol.hasInstance` for `instanceof` narrowing, include constructor as part of class structural type","articleBody":"My proposal:\r\n\r\n- For `instanceof` expressions, if `Symbol.hasInstance` exists on the RHS, narrow the expression according to the method's return value.\r\n- For classes, implicitly add a type-level `constructor` prototype property which is `typeof classConstructor`, if such a property was not defined in the class body.\r\n\r\n```ts\r\ninterface Foo { constructor: typeof Foo }\r\ndeclare const Foo: {\r\n [Symbol.hasInstance](x: any): x is Foo\r\n}\r\n\r\ndeclare const Bar: {\r\n [Symbol.hasInstance](x: any): x is number\r\n}\r\n\r\ndeclare const foo: any\r\ndeclare const bar: any\r\n\r\nif (foo instanceof Foo) {\r\n // foo should be inferred as a Foo\r\n}\r\n\r\nif (bar instanceof Bar) {\r\n // bar should be inferred as a number\r\n}\r\n```\r\n\r\nThe rationale of this is that you can then (via compiler flag) by default define `Symbol.hasInstance` on constructors to type check against their return type (like for `new () => T`, defining `[Symbol.hasInstance](x: any): x is T`). This would mostly mitigate a certain unsoundness issue with classes, making the workaround a bit more difficult to do:\r\n\r\n```ts\r\nclass Foo {\r\n bar: number = 1;\r\n}\r\n\r\nconst foo1: Foo = new Foo() // Legal\r\nconst foo2: Foo = {bar: 2} // No longer legal\r\n\r\n// Making these illegal are out of scope of this proposal\r\nconst foo3: Foo = {\r\n constructor: Foo;\r\n bar: 2,\r\n}\r\nconst foo4: Foo = new class {\r\n bar: number = 1\r\n}\r\n```\r\n\r\n-----\r\n\r\nNotes:\r\n\r\n- This has no effect on emit.\r\n- This has no effect on JS compatibility.\r\n- This will create new type errors, and thus will be moderately breaking.\r\n- This brings type semantics more in line with JS runtime semantics.\r\n- This *shouldn't* have a substantial effect on compiler performance, since the default `Symbol.hasInstance` is equivalent to legacy `instanceof`.\r\n- This would largely fix the premise of #17344 and would reduce the need for nominal typing (#202) in a common use case.","author":{"url":"https://github.com/dead-claudia","@type":"Person","name":"dead-claudia"},"datePublished":"2017-07-22T18:01:35Z","url":"https://github.com/microsoft/TypeScript/issues/17360"}
| 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:63780f0f-68ac-6904-d033-95a491b9d45e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8934:2DBB21:26A855C:339C554:6B01AD30 |
| html-safe-nonce | 2e6791c74521da7142d220cd422f39a24a8a8fbe767c39b2cbcce228d05d8a7e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4OTM0OjJEQkIyMToyNkE4NTVDOjMzOUM1NTQ6NkIwMUFEMzAiLCJ2aXNpdG9yX2lkIjoiMjA0MDg3NjExMDQ2NzY3MzM5MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 38690492ec3a01306e3dec9b6ce27274b62f5a22be6a22669a46e18edc17584d |
| hovercard-subject-tag | issue:244861972 |
| 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/17360/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ef2ddd0da4ce3ec38e1ad30afb05dd18a72dbb03d4bbad341fd04be716f950fc/microsoft/TypeScript/issues/17360 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ef2ddd0da4ce3ec38e1ad30afb05dd18a72dbb03d4bbad341fd04be716f950fc/microsoft/TypeScript/issues/17360 |
| og:image:alt | My proposal: For instanceof expressions, if Symbol.hasInstance exists on the RHS, narrow the expression according to the method's return value. For classes, implicitly add a type-level constructor ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | dead-claudia |
| 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