Title: instanceof should not narrow in false branch; or structural assignment to class types should not be allowed · Issue #33481 · microsoft/TypeScript · GitHub
Open Graph Title: instanceof should not narrow in false branch; or structural assignment to class types should not be allowed · Issue #33481 · microsoft/TypeScript
X Title: instanceof should not narrow in false branch; or structural assignment to class types should not be allowed · Issue #33481 · microsoft/TypeScript
Description: TypeScript Version: 3.5.1 Search Terms: instanceof, narrow, false branch Code class A { x! : number } class B { y! : number } //Neither A nor B, nominally //Is A, structurally const aOrB : A|B = { x : 32 }; if (aOrB instanceof A) { const...
Open Graph Description: TypeScript Version: 3.5.1 Search Terms: instanceof, narrow, false branch Code class A { x! : number } class B { y! : number } //Neither A nor B, nominally //Is A, structurally const aOrB : A|B = { ...
X Description: TypeScript Version: 3.5.1 Search Terms: instanceof, narrow, false branch Code class A { x! : number } class B { y! : number } //Neither A nor B, nominally //Is A, structurally const aOrB : A|B = { ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/33481
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"instanceof should not narrow in false branch; or structural assignment to class types should not be allowed","articleBody":"\u003c!-- 🚨 STOP 🚨 𝗦𝗧𝗢𝗣 🚨 𝑺𝑻𝑶𝑷 🚨\r\n\r\nHalf of all issues filed here are duplicates, answered in the FAQ, or not appropriate for the bug tracker. Even if you think you've found a *bug*, please read the FAQ first, especially the Common \"Bugs\" That Aren't Bugs section!\r\n\r\nPlease help us by doing the following steps before logging an issue:\r\n * Search: https://github.com/Microsoft/TypeScript/search?type=Issues\r\n * Read the FAQ: https://github.com/Microsoft/TypeScript/wiki/FAQ\r\n\r\nPlease fill in the *entire* template below.\r\n-->\r\n\r\n\u003c!-- Please try to reproduce the issue with `typescript@next`. It may have already been fixed. -->\r\n**TypeScript Version:** 3.5.1\r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms:**\r\n\r\ninstanceof, narrow, false branch\r\n\r\n**Code**\r\n\r\n```ts\r\nclass A {\r\n x! : number\r\n}\r\nclass B {\r\n y! : number\r\n}\r\n\r\n//Neither A nor B, nominally\r\n//Is A, structurally\r\nconst aOrB : A|B = { x : 32 };\r\n\r\nif (aOrB instanceof A) {\r\n const a : A = aOrB; //OK\r\n} else {\r\n const b : B = aOrB; //Expected to fail; is OK\r\n}\r\n```\r\n\r\n**Expected behavior:**\r\n\r\n`A|B` should not narrow to `B` in the `false` branch.\r\n\r\nIt is possible for `aOrB` to structurally match `A` but not nominally match `A`.\r\n`instanceof` only checks for a nominal match; not structural match.\r\n\r\n**Actual behavior:**\r\n\r\n`A|B` is narrowed to `B` in the `false` branch and will cause run-time bugs.\r\n\r\n**Playground Link:** [Playground](http://www.typescriptlang.org/play/#code/MYGwhgzhAECC0G8BQ1oA8CE0Bc0B2ArgLYBGApgE5IC+SokMAQoitAJ5a6GmU1JIB6AQDkyASwAuAC0px8AewrRGAGgVExeMCBBtBAgJIxYaiBIoFgEghW2668vGehgA8hWa5YAH2YBeRHQcaABmACZoagBufjEAM2gACjcPaE0zMDxgMnkE2ABKFlRgR2cwYPgAlMYo6CFXAGkaaDIQCDIi6BKnCWgSYP8Xdxq6gQBRNAAHMisyABNoCXloOLAxEFqxGEaaIA)\r\n\r\n**Related Issues:** \r\n\r\nhttps://github.com/microsoft/TypeScript/pull/32774\r\n\r\nI'm sure many people have known of this behaviour for a long time, and I never use classes or `instanceof` much. So, I never bothered to create an issue for it. However, the above PR finally motivated me to bring this up.\r\n\r\nI tried to look for \"instanceof narrow false branch\" on the issues page but couldn't find anything related.\r\n\r\nI'm sure this will get closed as \"Working as Intended\" or \"Duplicate\" but I feel like I needed to get this out of my system, anyway =x","author":{"url":"https://github.com/AnyhowStep","@type":"Person","name":"AnyhowStep"},"datePublished":"2019-09-18T03:26:50Z","url":"https://github.com/microsoft/TypeScript/issues/33481"}
| 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:ba61a64a-60e7-4172-f088-8f33939c3f3b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 978C:14F601:10D83BD:158B188:6B01029D |
| html-safe-nonce | 3168ea62124885a868528b7f6b816cbc8f27b96d0b09f7ac13db63d095c2db5a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NzhDOjE0RjYwMToxMEQ4M0JEOjE1OEIxODg6NkIwMTAyOUQiLCJ2aXNpdG9yX2lkIjoiNTk1ODI1Nzg4NDk5MDA3OTY0NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | a6a81acf730991664b06efd0b69b9db0400a95e477510f6df8aec0d7ea387ed2 |
| hovercard-subject-tag | issue:494965662 |
| 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/33481/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c0e5ac28e1a35ca339c27da7be619ff7cfca194092e4349ce2b4c10600df0ee7/microsoft/TypeScript/issues/33481 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c0e5ac28e1a35ca339c27da7be619ff7cfca194092e4349ce2b4c10600df0ee7/microsoft/TypeScript/issues/33481 |
| og:image:alt | TypeScript Version: 3.5.1 Search Terms: instanceof, narrow, false branch Code class A { x! : number } class B { y! : number } //Neither A nor B, nominally //Is A, structurally const aOrB : A|B = { ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | AnyhowStep |
| 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