Title: Strict equals false type guard doesn't work · Issue #31105 · microsoft/TypeScript · GitHub
Open Graph Title: Strict equals false type guard doesn't work · Issue #31105 · microsoft/TypeScript
X Title: Strict equals false type guard doesn't work · Issue #31105 · microsoft/TypeScript
Description: Code interface A { a: string; } interface B { b: string; } type MyType = A | B; function isA(x: MyType): x is A { return "a" in x; } function someFn(x: MyType) { if (!isA(x)) { console.log(x.b); // ^ <- works perfectly } if (isA(x) === f...
Open Graph Description: Code interface A { a: string; } interface B { b: string; } type MyType = A | B; function isA(x: MyType): x is A { return "a" in x; } function someFn(x: MyType) { if (!isA(x)) { console.log(x.b); //...
X Description: Code interface A { a: string; } interface B { b: string; } type MyType = A | B; function isA(x: MyType): x is A { return "a" in x; } function someFn(x: MyType) { if (!isA(x)) { console.lo...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/31105
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Strict equals false type guard doesn't work","articleBody":"**Code**\r\n\r\n```ts\r\ninterface A {\r\n a: string;\r\n}\r\n\r\ninterface B {\r\n b: string;\r\n}\r\n\r\ntype MyType = A | B;\r\n\r\nfunction isA(x: MyType): x is A {\r\n return \"a\" in x;\r\n}\r\n\r\nfunction someFn(x: MyType) {\r\n if (!isA(x)) {\r\n console.log(x.b);\r\n // ^ \u003c- works perfectly\r\n }\r\n\r\n if (isA(x) === false) {\r\n console.log(x.b);\r\n // ^ \u003c- here's the problem\r\n }\r\n}\r\n```\r\n\r\n**Expected behavior:**\r\n\r\nBeing able to access `x.b` in this if statement:\r\n\r\n```ts\r\nif (isA(x) === false) {\r\n console.log(x.b);\r\n}\r\n```\r\n\r\n**Actual behavior:**\r\n\r\nError: Property b does not exists on type `A`.\r\n\r\n**Playground Link:**\r\n\r\n[Playground](https://www.typescriptlang.org/play/index.html#src=interface%20A%20%7B%0D%0A%20%20a%3A%20string%3B%0D%0A%7D%0D%0A%0D%0Ainterface%20B%20%7B%0D%0A%20%20b%3A%20string%3B%0D%0A%7D%0D%0A%0D%0Atype%20MyType%20%3D%20A%20%7C%20B%3B%0D%0A%0D%0Afunction%20isA(x%3A%20MyType)%3A%20x%20is%20A%20%7B%0D%0A%20%20return%20%22a%22%20in%20x%3B%0D%0A%7D%0D%0A%0D%0Afunction%20someFn(x%3A%20MyType)%20%7B%0D%0A%20%20if%20(!isA(x))%20%7B%0D%0A%20%20%20%20console.log(x.b)%3B%0D%0A%20%20%20%20%2F%2F%20%20%20%20%20%20%20%20%20%20%20%20%5E%20%3C-%20works%20perfectly%0D%0A%20%20%7D%0D%0A%0D%0A%20%20if%20(isA(x)%20%3D%3D%3D%20false)%20%7B%0D%0A%20%20%20%20console.log(x.b)%3B%0D%0A%20%20%20%20%2F%2F%20%20%20%20%20%20%20%20%20%20%20%20%5E%20%3C-%20here's%20the%20problem%0D%0A%20%20%7D%0D%0A%7D)\r\n","author":{"url":"https://github.com/hansott","@type":"Person","name":"hansott"},"datePublished":"2019-04-25T07:39:37Z","url":"https://github.com/microsoft/TypeScript/issues/31105"}
| 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:f2d43e5d-9494-a6e9-60da-91f457ec8075 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B0A0:785A9:29E0605:3529E3A:6B01A580 |
| html-safe-nonce | 98768fd08acb212cf30dcc7f9abb95a43a6bb229d4fc22047d6e7f300d7b5777 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMEEwOjc4NUE5OjI5RTA2MDU6MzUyOUUzQTo2QjAxQTU4MCIsInZpc2l0b3JfaWQiOiI3MDQ1ODkzMTc5ODQ1OTMyODAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | ec6d46319e75ffff70140c8a54e72b9ed7365c32f844622898d5c8942adac7a2 |
| hovercard-subject-tag | issue:437051946 |
| 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/31105/issue_layout |
| twitter:image | https://opengraph.githubassets.com/304364142179ead59a181c716fb08f996ed138b390b70773185604396339ba40/microsoft/TypeScript/issues/31105 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/304364142179ead59a181c716fb08f996ed138b390b70773185604396339ba40/microsoft/TypeScript/issues/31105 |
| og:image:alt | Code interface A { a: string; } interface B { b: string; } type MyType = A | B; function isA(x: MyType): x is A { return "a" in x; } function someFn(x: MyType) { if (!isA(x)) { console.log(x.b); //... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | hansott |
| 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