Title: [TS 5.4.0-beta] `instaceof` narrowing impacted by prototype property · Issue #57397 · microsoft/TypeScript · GitHub
Open Graph Title: [TS 5.4.0-beta] `instaceof` narrowing impacted by prototype property · Issue #57397 · microsoft/TypeScript
X Title: [TS 5.4.0-beta] `instaceof` narrowing impacted by prototype property · Issue #57397 · microsoft/TypeScript
Description: 🔎 Search Terms instanceof, narrowing, prototype 🕗 Version & Regression Information This changed between versions 5.3.3 and 5.4.0-beta. This changed on 5.4.0-dev.20231130 (commits) ⏯ Playground Link https://www.typescriptlang.org/play?tar...
Open Graph Description: 🔎 Search Terms instanceof, narrowing, prototype 🕗 Version & Regression Information This changed between versions 5.3.3 and 5.4.0-beta. This changed on 5.4.0-dev.20231130 (commits) ⏯ Playground Link...
X Description: 🔎 Search Terms instanceof, narrowing, prototype 🕗 Version & Regression Information This changed between versions 5.3.3 and 5.4.0-beta. This changed on 5.4.0-dev.20231130 (commits) ⏯ Playground ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/57397
X: @github
Domain: github.com
IPv4 address: 140.82.112.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[TS 5.4.0-beta] `instaceof` narrowing impacted by prototype property","articleBody":"### 🔎 Search Terms\r\n\r\ninstanceof, narrowing, prototype\r\n\r\n### 🕗 Version & Regression Information\r\n\r\n- This changed between versions 5.3.3 and 5.4.0-beta. \r\n- This changed on 5.4.0-dev.20231130 ([commits](https://github.com/microsoft/TypeScript/compare/2869579b2cad770ae20617e4148741c2ce35ffdc...7216ee0bb821b4b565d54c52dcbf788a3fdf7062))\r\n\r\n\r\n\r\n### ⏯ Playground Link\r\n\r\nhttps://www.typescriptlang.org/play?target=99&ts=5.4.0-dev.20231130#code/CYUwxgNghgTiAEkoGdnwOoEtgHMQBd4BvAKHnNGXxgHsBPACgEoAueANxuwG4SBfEiVBI4iGgDsqGbHnwBhCVRgBXMPhow2pcuXEgA7szZZcBXgKHhoosIsIBZEOOUnZc6Ki1kdew62LeOjqYyI7OADIh+GwARjQ0ECBQ4rxBfPAAZNKm+Kk6AA606vh0+SBsJWU0AGbZbnYqahrc8AD0rfAAPAC03fBwALY07JjiOPD4ABYh8FCc2GggMLQw-LwklQhhLjIE8AC88ACSkvjJYCAAKqUgnZs18NuuBO4oyAB868LWCLanj04djkKjcHk9dvIPGgstofAYjADnM98GtBO10RjMVjsTjcXj0YJEoQ-vgnNFEUDZPAAD7wZTiUDVUYgYC8b6wX52DjGCHrTC1BjseCjKjnEBgwHIpgBILkElkg4cFrtLq9eC+eBLFbC8TwACsADoACwGgAM3RiBCg-EEIAAHvkNIQiAIgA\r\n\r\n### 💻 Code\r\n\r\n(reduced down from large repo as best I could)\r\n\r\n```ts\r\ndeclare class Widget {\r\n destroy(): void;\r\n}\r\n\r\ndeclare const WidgetConstructor: {\r\n new(): Widget;\r\n}\r\n\r\ndeclare const MenuWidgetClass: {\r\n new(): {\r\n isMenuList: boolean;\r\n } & Widget;\r\n prototype: typeof WidgetConstructor; // \u003c-- removing this avoids error\r\n};\r\n\r\ntype MenuWidget = InstanceType\u003ctypeof MenuWidgetClass>;\r\n\r\ndeclare const MenuWidget: typeof MenuWidgetClass & {\r\n new(): MenuWidget\r\n};\r\n\r\n/////////////////////////////////////////////////////////\r\n\r\nlet content: MenuWidget | undefined;\r\ndeclare const v: Widget;\r\n\r\nif (v instanceof MenuWidget) {\r\n content = v; // \u003c-- new error in 5.4.0-beta\r\n}\r\n```\r\n\r\n\r\n### New behavior\r\n\r\nTypeScript `5.4.0-beta` appears to be consulting the `.prototype` property of the type in a way that it wasn't before resulting in the `instanceof` narrowing to narrow to the base class.\r\n\r\n### Previous behavior\r\n\r\nThe code in `5.3.3` was expecting the `instanceof` to narrow to the return type of the function being consulted.\r\n\r\n### Additional information about the issue\r\n\r\nNote: this is more of a change report than a bug report, as I don't think this was announced in the 5.4.0-beta breaking changes. I suspect the change is an improvement and is catching a potential issue that was previously unsafe.","author":{"url":"https://github.com/acutmore","@type":"Person","name":"acutmore"},"datePublished":"2024-02-13T15:39:03Z","url":"https://github.com/microsoft/TypeScript/issues/57397"}
| 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:422f871a-519e-e888-bd4d-3be125871c3d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E724:2AE08B:12050A:180F5D:6B04E2B9 |
| html-safe-nonce | 92711cf04bde6f2a5c4a16d17026367625046ac1342b393a1489877c51fdb2ed |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNzI0OjJBRTA4QjoxMjA1MEE6MTgwRjVEOjZCMDRFMkI5IiwidmlzaXRvcl9pZCI6Ijg0NzQyMDQ2NTEzMDUzNjIxMDUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 9fe948fe8f1cc13c3c730492f3a911547f0b3f7e5f7663746e18fbd341adea4e |
| hovercard-subject-tag | issue:2132606546 |
| 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/57397/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3b3e867cf9b5e25373756bb7f3f3eddfddb054bd2bd41bfc2efb543f5d5c8432/microsoft/TypeScript/issues/57397 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3b3e867cf9b5e25373756bb7f3f3eddfddb054bd2bd41bfc2efb543f5d5c8432/microsoft/TypeScript/issues/57397 |
| og:image:alt | 🔎 Search Terms instanceof, narrowing, prototype 🕗 Version & Regression Information This changed between versions 5.3.3 and 5.4.0-beta. This changed on 5.4.0-dev.20231130 (commits) ⏯ Playground Link... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | acutmore |
| hostname | github.com |
| expected-hostname | github.com |
| None | 970f03f35f02bb123d2b6f1884601d89a570dc02e5ef9612b1b6ca5c033b1796 |
| 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 | ebcec4199c8521b643c068ef118a1ba9a43b678a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width