Title: Incorrect narrowing when a type guard returns a type union · Issue #41871 · microsoft/TypeScript · GitHub
Open Graph Title: Incorrect narrowing when a type guard returns a type union · Issue #41871 · microsoft/TypeScript
X Title: Incorrect narrowing when a type guard returns a type union · Issue #41871 · microsoft/TypeScript
Description: TypeScript Version: 4.1.2 Search Terms: narrowing narrowed type guard type union Code MickeyPhoenix encountered this bug while implementing type guards for HBOMax data services. The original repro involves class inheritance, like this: p...
Open Graph Description: TypeScript Version: 4.1.2 Search Terms: narrowing narrowed type guard type union Code MickeyPhoenix encountered this bug while implementing type guards for HBOMax data services. The original repro ...
X Description: TypeScript Version: 4.1.2 Search Terms: narrowing narrowed type guard type union Code MickeyPhoenix encountered this bug while implementing type guards for HBOMax data services. The original repro ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/41871
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Incorrect narrowing when a type guard returns a type union","articleBody":"**TypeScript Version:** 4.1.2\r\n\r\n**Search Terms:**\r\n\r\n`narrowing` `narrowed` `type guard` `type union`\r\n\r\n**Code**\r\n\r\n@MickeyPhoenix encountered this bug while implementing type guards for HBOMax data services. The original repro involves `class` inheritance, like this:\r\n\r\n[playground link](https://www.typescriptlang.org/play?#code/MYGwhgzhAECCB2BLAtmE0DeAoavoAcAnRANzABcBTaADwC5p4BXZAI0sOgF5oAGAbiwBfLKEgwAwhWiUaVeABMYCFGkw48+JqxCJg0AO4ALRBADWHCA2ZsO3aAEZBIsVGgAhQgHsvydXgJiMipoAE9rFnZOHicsDVwtHT1DE3NLCNtox0EAxN19IzBFEEoMqK4AZmc4gDMmeGByRC94aEKIAHVUi0IIAApEeC1yBhVUdAAfD29fAEoGQeHoU2gpcmgpzx8-bADCSnImQlbFpnXBiHIi4EovGtXpCanT8-hL69v7rd9qrDqGpotZYNLyEfaNAZDM6jJDjDbTbbzRiROy7PCIe59dpdUw9fovWazfwBPAAelJ0AAkgA5CQAeQASgyAKISAAqDDZRg41AgRi8TBACmg7GgYGgwF8+EQJU4HG8nG5+wANNB4iTySKztAAAYvHXQADmXlekrBlEaIFCjDAYK8BkowvIXl132QBvVAU1kMulDAwruurW8LdOtmnrw+0Ox2BwwAdIVipR+GqAkJU5GDkdWgBaWIiP71RrNVpm8HkSHDGGqSYIuZlVHqjHQSvai5XBqfB7rJ6xttvDs3QNuoloknQTX0pmsjm6xMKEoGlbwE1i6DISiZaCBnXBzYzd3KiO4TUQF3kbkSqUyuxli3kK3Qfb4UHkGBFGR2xU8uPHp9ZmMXgTIoF2TdV0ziPYANzfM4klAcJWkHh4EoAxuz6WZBE1AAFYh4DfaAACJ6gUSgakGR1CJFC0wCYCBqGAChgKTaAFEQBR4AAcnWMBGiYNBH1kUxyFEFozxKOMQC8Q1ITvCFGPIQlBCAA)\r\n```ts\r\nclass Animal {\r\n private x: number = 0;\r\n}\r\nclass Cat extends Animal {\r\n public whiskers: number = 1;\r\n}\r\nclass Broom {\r\n private y: number = 1;\r\n\r\n public whiskers: number = 1;\r\n public handle: number=3;\r\n}\r\n\r\nfunction hasWhiskers(input: Animal | Broom): input is Cat | Broom {\r\n return input instanceof Cat || input instanceof Broom;\r\n}\r\n\r\nfunction incorrect(input: Animal | Broom): number {\r\n if (hasWhiskers(input)) {\r\n // INCORRECT: There should be a compiler error here, \r\n // but `input` got incorrectly narrowed to `Broom` \r\n // (instead of `Cat | Broom`)\r\n return input.handle; \r\n } \r\n return -1;\r\n}\r\n\r\nfunction correct(input: Animal | Broom): number {\r\n if (input instanceof Cat || input instanceof Broom) {\r\n // CORRECT: `handle` is not a member of `Cat | Broom`,\r\n // so the compiler correctly reports an error here.\r\n return input.handle;\r\n } \r\n\r\n return -1;\r\n}\r\n\r\nconst cat = new Cat();\r\n// Prints \"undefined\" because cat.handle didn't actually exist\r\nconsole.log(incorrect(cat));\r\n```\r\n\r\nBut it can also be reproduced using simpler `interface` inheritance, like this:\r\n\r\n[playground link](https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgIImAWzgG2QbwChkTkcIBzAZwC5kQBXTAI2gG5CBfQ0SWRFAGE4YZBAAekEABMqaDNjxFSyAO4ALYFQDW0WvSasoHbr2jwkyAEJQA9rcwFipDVt1R9jFu2cl1cGXI6LyMTQkIYBhAEMGBbEGR-KgB1TR09AApQAAcGMDp0LFxkAB9rOwcASjocvOQtZGFRMpt7R2VSKAgwBigEgHlmACsIGIA6JP7VEAAFO2zoMABPMYRcHCyQXLAAGmQAcld0j33KsIiomLiE0ARbKC6Yze2ChWKWisxqg28oJxVgDBkBkkqk3JlamBKpV-ipSAB6eHIACSADlBP0AEqYgCiggAKnR8epoCgqOpbAwcNJkKxkHBkHdMNlgOQ-tA7H8SV09r44YjaXUAAaQoXICi2US3e6PMA4Jb0OAPWyqCA0sC2ZBC1oOMV8lQCzZUSBwGm2IFCpqlcptIWVfWdbq9G5bPITALSchsZB8zg+lRdHp9ZAAWgAjGEgA)\r\n```ts\r\ninterface Animal {\r\n legs: number;\r\n}\r\ninterface Cat extends Animal {\r\n whiskers: number;\r\n}\r\ninterface Broom {\r\n whiskers: number;\r\n handle: number;\r\n}\r\n\r\nfunction hasWhiskers(input: Animal | Broom): input is Cat | Broom {\r\n return Object.hasOwnProperty.call(input, 'whiskers');\r\n}\r\n\r\nfunction incorrect(input: Animal | Broom): number {\r\n if (hasWhiskers(input)) {\r\n // INCORRECT: There should be a compiler error here, \r\n // but `input` got incorrectly narrowed to `Broom` \r\n // (instead of `Cat | Broom`)\r\n return input.handle; \r\n } \r\n return -1;\r\n}\r\n\r\n```\r\n\r\n**Expected behavior:**\r\n\r\nOne would expect identical type narrowing when extracting this:\r\n```ts\r\nfunction f(input: Animal | Broom): number {\r\n if (input instanceof Cat || input instanceof Broom) {\r\n . . .\r\n```\r\n...into this:\r\n```ts\r\nfunction hasWhiskers(input: Animal | Broom): input is Cat | Broom {\r\n return input instanceof Cat || input instanceof Broom;\r\n}\r\n\r\n. . .\r\n\r\nfunction f(input: Animal | Broom): number {\r\n if (hasWhiskers(input)) {\r\n . . .\r\n```\r\n\r\n**Actual behavior:**\r\n\r\nThe type is incorrectly narrowed to `Broom` instead of `Cat | Broom`.\r\n\r\nAs a result, clearly incorrect code compiles without any error.\r\n\r\n**Related Issues:** https://github.com/microsoft/TypeScript/issues/31156\r\n","author":{"url":"https://github.com/octogonz","@type":"Person","name":"octogonz"},"datePublished":"2020-12-08T05:42:05Z","url":"https://github.com/microsoft/TypeScript/issues/41871"}
| 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:b100d6f1-9d8f-33ee-4d67-1327553d0b58 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DC20:4BB79:1A6E766:21B268C:6B01533B |
| html-safe-nonce | fd212ca0917ae0bcbfce6c5ce1f51ba7b84a169494886e186ad1960f276085cf |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEQzIwOjRCQjc5OjFBNkU3NjY6MjFCMjY4Qzo2QjAxNTMzQiIsInZpc2l0b3JfaWQiOiIxMjMwOTI3Mjg2NTQ3NjAwMTg3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 16f4308c49cc2731edd1b5ab05057d812579e3b17d1e84d46bdbb47a59733424 |
| hovercard-subject-tag | issue:759083558 |
| 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/41871/issue_layout |
| twitter:image | https://opengraph.githubassets.com/534f9f8ba5e4e155fb54da0e68482450c005b5472bb90fd986ad0b4f26b934c5/microsoft/TypeScript/issues/41871 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/534f9f8ba5e4e155fb54da0e68482450c005b5472bb90fd986ad0b4f26b934c5/microsoft/TypeScript/issues/41871 |
| og:image:alt | TypeScript Version: 4.1.2 Search Terms: narrowing narrowed type guard type union Code MickeyPhoenix encountered this bug while implementing type guards for HBOMax data services. The original repro ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | octogonz |
| 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