Title: Different narrowing behaviour of user-defined type guards vs discriminated unions · Issue #10145 · microsoft/TypeScript · GitHub
Open Graph Title: Different narrowing behaviour of user-defined type guards vs discriminated unions · Issue #10145 · microsoft/TypeScript
X Title: Different narrowing behaviour of user-defined type guards vs discriminated unions · Issue #10145 · microsoft/TypeScript
Description: TypeScript Version: nightly (2.1.0-dev.20160804) Code interface A { type: 'A' } interface B { type: 'B' } function isA(x: A|B): x is A { return x.type === 'A'; } function isB(x: A|B): x is B { return x.type === 'B'; } // Using if/else wi...
Open Graph Description: TypeScript Version: nightly (2.1.0-dev.20160804) Code interface A { type: 'A' } interface B { type: 'B' } function isA(x: A|B): x is A { return x.type === 'A'; } function isB(x: A|B): x is B { retu...
X Description: TypeScript Version: nightly (2.1.0-dev.20160804) Code interface A { type: 'A' } interface B { type: 'B' } function isA(x: A|B): x is A { return x.type === 'A'; } function is...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/10145
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Different narrowing behaviour of user-defined type guards vs discriminated unions","articleBody":"**TypeScript Version:** nightly (2.1.0-dev.20160804)\n\n**Code**\n\n``` ts\n\ninterface A { type: 'A' }\ninterface B { type: 'B' }\n\nfunction isA(x: A|B): x is A { return x.type === 'A'; }\nfunction isB(x: A|B): x is B { return x.type === 'B'; }\n\n// Using if/else with type guard functions\nfunction foo1(x: A|B): any {\n x // x is A | B\n if (isA(x)) {\n return x; // x is A\n }\n x // x is B |\n if (isB(x)) {\n return x; // x is B\n }\n x // x is B, but should be never \u003c===== (1)\n}\n\n// Using if/else with discriminated unions\nfunction foo2(x: A|B): any {\n x // x is A | B\n if (x.type === 'A') {\n return x; // x is A\n }\n x // x is B\n if (x.type === 'B') {\n return x; // x is B\n }\n x // x is never \u003c===== (2)\n}\n```\n\n**Expected behavior:**\n`x` is narrowed to `never` at both (1) and (2)\n\n**Actual behavior:** \n`x` is narrowed to `never` at (2), but at (1) it is narrowed to `B`.\n\n**Notes:**\nThis is carried over from #9260 which is closed. See https://github.com/Microsoft/TypeScript/issues/9260#issuecomment-237564186\n","author":{"url":"https://github.com/yortus","@type":"Person","name":"yortus"},"datePublished":"2016-08-04T15:00:59Z","url":"https://github.com/microsoft/TypeScript/issues/10145"}
| 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:6d0d213f-e74e-4715-82b6-afb440a25989 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D90E:3A05E7:18983C:205FF3:6B0163C1 |
| html-safe-nonce | cfff7129bf01f662f58767cf7fb2debed3d75117ef3169a3fb873bdf96ee818a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEOTBFOjNBMDVFNzoxODk4M0M6MjA1RkYzOjZCMDE2M0MxIiwidmlzaXRvcl9pZCI6Ijc2NTcxNjM2NTUyNjMzODA0MTciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | d1e3307e8a61d247f94aa8608ee9807346931ff34f85557110a88aea00e17936 |
| hovercard-subject-tag | issue:169396258 |
| 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/10145/issue_layout |
| twitter:image | https://opengraph.githubassets.com/44520ede4a6bfef12831e3dda3e69d260f88cbd7991e526b0954e7a8e5623154/microsoft/TypeScript/issues/10145 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/44520ede4a6bfef12831e3dda3e69d260f88cbd7991e526b0954e7a8e5623154/microsoft/TypeScript/issues/10145 |
| og:image:alt | TypeScript Version: nightly (2.1.0-dev.20160804) Code interface A { type: 'A' } interface B { type: 'B' } function isA(x: A|B): x is A { return x.type === 'A'; } function isB(x: A|B): x is B { retu... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | yortus |
| 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