Title: typeguards only narrow types when used with unary not · Issue #44366 · microsoft/TypeScript · GitHub
Open Graph Title: typeguards only narrow types when used with unary not · Issue #44366 · microsoft/TypeScript
X Title: typeguards only narrow types when used with unary not · Issue #44366 · microsoft/TypeScript
Description: Bug Report 🔎 Search Terms negation typeguard unary not 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about narrowing/typeguards The FAQ entry with the most promising ...
Open Graph Description: Bug Report 🔎 Search Terms negation typeguard unary not 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about narrowing/typeguard...
X Description: Bug Report 🔎 Search Terms negation typeguard unary not 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about narrowing/typeg...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/44366
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"typeguards only narrow types when used with unary not","articleBody":"# Bug Report\r\n\r\n### 🔎 Search Terms\r\n\r\n- negation\r\n- typeguard\r\n- unary not\r\n\r\n### 🕗 Version & Regression Information\r\n\r\n- This is the behavior in every version I tried, and I reviewed the FAQ for entries about narrowing/typeguards\r\n\r\nThe FAQ entry with the most promising title is a `TODO`: https://github.com/Microsoft/TypeScript/wiki/FAQ#why-doesnt-isfoox-narrow-x-to-foo-when-isfoo-is-a-type-guard\r\n\r\nThis occurs on every version of Typescript available in the playground, including nightly.\r\n\r\n### ⏯ Playground Link\r\n\r\n[Playground Link](https://www.typescriptlang.org/play?ts=4.3.2#code/JYOwLgpgTgZghgYwgAgKLmGAnsg3gKGWWwAcIAuZAZzClAHMBufAX33wQHsQbkBBAMIAVAPIAlAPpCAmgAVUyALzIA5IjCcoK5vlCRYiFHwQaoyCAA9IIACZU0GbHkLEsZSqQicY-YeKlyqMxs+DAAriAmwNzIwFTGpgAUEI5YlOhgmFgAlJQpmU5x-CaazkRQEGBhUCDmqQB0nkqKyoKikjLywexcPGDIMJyclAmlyrielGolWiyMyAD0C8gAbnAANsA2ADTIcPaWZCYQNhzcvABGcFAjM0p4k6ognGAAtGAAFnBgKnOLy6A1psdnsDhYjpBTr1LqB0ql7hM3BRVJgIABbX7zJarDZbXbPfpwWrqTTsYA+RJxUZQRIXUDZZrKeDrKgQBkEIiebzIOkgLEA+wZLK7fbmcEQY6nFjmFkoDmuMjc3n82KC1L4l5iiEnVhkilUma0+nNAYbVnslxcnzK1gy1llBVea2gFVFIXYXZUOBolCiuAXTgrCC63QUgCEBqSvOyFs5SKVLv+qocBSwIrB2qldrllvjzr5SaK1PTWolkNYQA)\r\n\r\n### 💻 Code\r\n\r\n```ts\r\ninterface Entity {\r\n type: string;\r\n}\r\n\r\nconst ACTOR_TYPE = 'actor';\r\n\r\ninterface Actor extends Entity {\r\n type: typeof ACTOR_TYPE;\r\n}\r\n\r\nfunction isActor(entity: Entity): entity is Actor {\r\n return entity.type === ACTOR_TYPE;\r\n}\r\n\r\nconst foo: Actor = {type: 'actor'}; // valid, as expected\r\nconst bar: Actor = {type: 'not-that'}; // invalid, as expected\r\nconst bin: Entity = {type: 'item'}; // valid, not an actor\r\n\r\nif (isActor(bin) === false) {\r\n typeof bin; // is Entity, as expected\r\n} else {\r\n typeof bin; // is Entity, not as expected\r\n}\r\n\r\nif (isActor(bin) == false) {\r\n typeof bin;\r\n} else {\r\n typeof bin; // unexpected Entity, same as above\r\n}\r\n\r\nif (!isActor(bin)) {\r\n typeof bin; // is Entity, as expected\r\n} else {\r\n typeof bin; // is Actor, as expected\r\n}\r\n```\r\n\r\n### 🙁 Actual behavior\r\n\r\nComparing the result of a typeguard with `guard(it) === false` or `== false` does not narrow the type, but the unary operator with `!guard(it)` does narrow it to `Actor`.\r\n\r\n### 🙂 Expected behavior\r\n\r\nThis may be due to a subtlety with the `entity is Actor` return type not being a proper boolean, but I would expect all forms of negation and comparison to `false` to narrow the type, and https://www.typescriptlang.org/docs/handbook/2/narrowing.html#equality-narrowing seems to support that. For a typeguard that returns a boolean, the comparisons should be equivalent. Is there some reason that is not the case here?\r\n\r\nI'm working with lint rules that discourage unary not (easy to miss) and discovered this while doing some refactoring.\r\n","author":{"url":"https://github.com/ssube","@type":"Person","name":"ssube"},"datePublished":"2021-06-01T16:31:02Z","url":"https://github.com/microsoft/TypeScript/issues/44366"}
| 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:9c6ac9e9-86fb-bca0-9b15-dad7599c9d88 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CB76:2E6CF7:7EB5:A5AA:6B017B5A |
| html-safe-nonce | 6944064111b6add6fe9ed605678d9509c0d7b8c564e3377c260237277d99eb07 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQjc2OjJFNkNGNzo3RUI1OkE1QUE6NkIwMTdCNUEiLCJ2aXNpdG9yX2lkIjoiNzcyNTE2NTE1MDkwNjQ0ODczMCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 765532ccd9352d6e2d628d83ae6c1585d8577b2d13a7de3d6f998694b5958f02 |
| hovercard-subject-tag | issue:908482399 |
| 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/44366/issue_layout |
| twitter:image | https://opengraph.githubassets.com/532c15d8055fd64faf4d36503fe80002ecdd9862674eaee4a2d2167ff726e0c3/microsoft/TypeScript/issues/44366 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/532c15d8055fd64faf4d36503fe80002ecdd9862674eaee4a2d2167ff726e0c3/microsoft/TypeScript/issues/44366 |
| og:image:alt | Bug Report 🔎 Search Terms negation typeguard unary not 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about narrowing/typeguard... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ssube |
| 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