René's URL Explorer Experiment


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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:9c6ac9e9-86fb-bca0-9b15-dad7599c9d88
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idCB76:2E6CF7:7EB5:A5AA:6B017B5A
html-safe-nonce6944064111b6add6fe9ed605678d9509c0d7b8c564e3377c260237277d99eb07
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQjc2OjJFNkNGNzo3RUI1OkE1QUE6NkIwMTdCNUEiLCJ2aXNpdG9yX2lkIjoiNzcyNTE2NTE1MDkwNjQ0ODczMCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac765532ccd9352d6e2d628d83ae6c1585d8577b2d13a7de3d6f998694b5958f02
hovercard-subject-tagissue:908482399
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/44366/issue_layout
twitter:imagehttps://opengraph.githubassets.com/532c15d8055fd64faf4d36503fe80002ecdd9862674eaee4a2d2167ff726e0c3/microsoft/TypeScript/issues/44366
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/532c15d8055fd64faf4d36503fe80002ecdd9862674eaee4a2d2167ff726e0c3/microsoft/TypeScript/issues/44366
og:image:altBug 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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamessube
hostnamegithub.com
expected-hostnamegithub.com
Nonecc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c
turbo-cache-controlno-preview
go-importgithub.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git
octolytics-dimension-user_id6154722
octolytics-dimension-user_loginmicrosoft
octolytics-dimension-repository_id20929025
octolytics-dimension-repository_nwomicrosoft/TypeScript
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id20929025
octolytics-dimension-repository_network_root_nwomicrosoft/TypeScript
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release8c77c9e8439031ad54c75a250694842892f693e1
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/microsoft/TypeScript/issues/44366#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F44366
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F44366
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=microsoft%2FTypeScript
Reloadhttps://github.com/microsoft/TypeScript/issues/44366
Reloadhttps://github.com/microsoft/TypeScript/issues/44366
Reloadhttps://github.com/microsoft/TypeScript/issues/44366
microsofthttps://github.com/microsoft
TypeScripthttps://github.com/microsoft/TypeScript
Codehttps://github.com/microsoft/TypeScript
Issues5k (5k)https://github.com/microsoft/TypeScript/issues
Pull requests312 (312)https://github.com/microsoft/TypeScript/pulls
Actionshttps://github.com/microsoft/TypeScript/actions
Projectshttps://github.com/microsoft/TypeScript/projects
Wikihttps://github.com/microsoft/TypeScript/wiki
Security and qualityhttps://github.com/microsoft/TypeScript/security
Insightshttps://github.com/microsoft/TypeScript/insights
https://github.com/microsoft/TypeScript
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/44366
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/44366
#53714https://github.com/microsoft/TypeScript/pull/53714
typeguards only narrow types when used with unary nothttps://github.com/microsoft/TypeScript/issues/44366#top
#53714https://github.com/microsoft/TypeScript/pull/53714
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featurehttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Awaiting%20More%20Feedback%22
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%22
https://github.com/ssube
Sean Sube (ssube)https://github.com/ssube
on Jun 1, 2021https://github.com/microsoft/TypeScript/issues/44366#issue-908482399
https://github.com/Microsoft/TypeScript/wiki/FAQ#why-doesnt-isfoox-narrow-x-to-foo-when-isfoo-is-a-type-guardhttps://github.com/Microsoft/TypeScript/wiki/FAQ#why-doesnt-isfoox-narrow-x-to-foo-when-isfoo-is-a-type-guard
Playground Linkhttps://www.typescriptlang.org/play?ts=4.3.2#code/JYOwLgpgTgZghgYwgAgKLmGAnsg3gKGWWwAcIAuZAZzClAHMBufAX33wQHsQbkBBAMIAVAPIAlAPpCAmgAVUyALzIA5IjCcoK5vlCRYiFHwQaoyCAA9IIACZU0GbHkLEsZSqQicY-YeKlyqMxs+DAAriAmwNzIwFTGpgAUEI5YlOhgmFgAlJQpmU5x-CaazkRQEGBhUCDmqQB0nkqKyoKikjLywexcPGDIMJyclAmlyrielGolWiyMyAD0C8gAbnAANsA2ADTIcPaWZCYQNhzcvABGcFAjM0p4k6ognGAAtGAAFnBgKnOLy6A1psdnsDhYjpBTr1LqB0ql7hM3BRVJgIABbX7zJarDZbXbPfpwWrqTTsYA+RJxUZQRIXUDZZrKeDrKgQBkEIiebzIOkgLEA+wZLK7fbmcEQY6nFjmFkoDmuMjc3n82KC1L4l5iiEnVhkilUma0+nNAYbVnslxcnzK1gy1llBVea2gFVFIXYXZUOBolCiuAXTgrCC63QUgCEBqSvOyFs5SKVLv+qocBSwIrB2qldrllvjzr5SaK1PTWolkNYQA
https://www.typescriptlang.org/docs/handbook/2/narrowing.html#equality-narrowinghttps://www.typescriptlang.org/docs/handbook/2/narrowing.html#equality-narrowing
https://github.com/MartinJohns
Martin Johns (MartinJohns)https://github.com/MartinJohns
on Jun 1, 2021https://github.com/microsoft/TypeScript/issues/44366#issuecomment-852347178
#32819 (comment)https://github.com/microsoft/TypeScript/issues/32819#issuecomment-520543814
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featurehttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Awaiting%20More%20Feedback%22
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%22
on Jun 1, 2021https://github.com/microsoft/TypeScript/issues/44366#event-4827151575
https://github.com/ssube
Sean Sube (ssube)https://github.com/ssube
on Jun 1, 2021https://github.com/microsoft/TypeScript/issues/44366#issuecomment-852384316
#32819 (comment)https://github.com/microsoft/TypeScript/issues/32819#issuecomment-522319066
https://github.com/MartinJohns
Martin Johns (MartinJohns)https://github.com/MartinJohns
on Jun 1, 2021https://github.com/microsoft/TypeScript/issues/44366#issuecomment-852445523
#32819 (comment)https://github.com/microsoft/TypeScript/issues/32819#issuecomment-522319066
https://github.com/ssube
Sean Sube (ssube)https://github.com/ssube
on Jun 1, 2021https://github.com/microsoft/TypeScript/issues/44366#issuecomment-852455668
https://github.com/RyanCavanaugh
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
on Jun 1, 2021https://github.com/microsoft/TypeScript/issues/44366#issuecomment-852508690
https://github.com/nmain
Nicholai Main (nmain)https://github.com/nmain
on Jun 2, 2021https://github.com/microsoft/TypeScript/issues/44366#issuecomment-853036464
https://github.com/Craigfis
Craig Fisher (Craigfis)https://github.com/Craigfis
on Jun 4, 2021https://github.com/microsoft/TypeScript/issues/44366#issuecomment-855122768
Playgroundhttps://www.typescriptlang.org/play?jsx=0&strict=true#code/FAFwngDgpgBAgjAvDA3sGMDWBLAdgEwC4YByAQxIG5gBfUSWAIQ2TTuAGMB7XAZxBjZeCZAAoAHsQQAfGIwCUxcYN7wkAPhgB5AEYArKBxAA6CACcuISw2MALMry0B3XAAUL0M+GMcyAGz8JABpSHAISeWpuPgEhZjFJNVkFJRU5DRgAQiE4CUjgTh5+GAAzJBgJKRhkxST0xE00DGwS0WzhPPkmjBgzKBAAVzNcGHFKDAB6CZhwaDl0GBooP15Yboxo4o4q5DGYKdnYOAWMPsHhmA5jMPxqDDo6TYEAcwBGcsq6lK+M7pbROKdda9fpDEZ7KYwAAqDC+CyWKzWJ0uRQE2zUu3GkJhc1wUAAblAzMizmDLtc8Ld4bRCjEYM8AEwfRIyOS1VnxRoLf7tXLieRdEmgi57fbTHFMeHLVaoZFPS47UZY8Ww449EHnEZXG53Ra0IA
https://github.com/ssube
Sean Sube (ssube)https://github.com/ssube
on Jun 10, 2021https://github.com/microsoft/TypeScript/issues/44366#issuecomment-858785589
Nicholai Main (@nmain)https://github.com/nmain
https://eslint.org/docs/rules/no-restricted-syntaxhttps://eslint.org/docs/rules/no-restricted-syntax
https://github.com/clshortfuse
Carlos Lopez (clshortfuse)https://github.com/clshortfuse
on Feb 14, 2022https://github.com/microsoft/TypeScript/issues/44366#issuecomment-1039481671
TypeScript/src/compiler/binder.tshttps://github.com/microsoft/TypeScript/blob/f84a67f29cfafa8844ccf904e97770847fb50d8d/src/compiler/binder.ts#L929-L933
f84a67fhttps://github.com/microsoft/TypeScript/commit/f84a67f29cfafa8844ccf904e97770847fb50d8d
https://github.com/ngregory-rbi
ngregory-rbihttps://github.com/ngregory-rbi
on Dec 16, 2022https://github.com/microsoft/TypeScript/issues/44366#issuecomment-1355119677
https://github.com/ljharb
Jordan Harband (ljharb)https://github.com/ljharb
on Feb 27, 2023https://github.com/microsoft/TypeScript/issues/44366#issuecomment-1447087218
#53005https://github.com/microsoft/TypeScript/issues/53005
Jake Bailey (jakebailey)https://github.com/jakebailey
completedhttps://github.com/microsoft/TypeScript/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
#53714https://github.com/microsoft/TypeScript/pull/53714
on Sep 19, 2023https://github.com/microsoft/TypeScript/issues/44366#event-10415127410
https://github.com/ljharb
Jordan Harband (ljharb)https://github.com/ljharb
on Sep 19, 2023https://github.com/microsoft/TypeScript/issues/44366#issuecomment-1726443825
https://github.com/jakebailey
Jake Bailey (jakebailey)https://github.com/jakebailey
on Sep 19, 2023https://github.com/microsoft/TypeScript/issues/44366#issuecomment-1726445176
#55486https://github.com/microsoft/TypeScript/issues/55486
Sign up for freehttps://github.com/signup?return_to=https://github.com/microsoft/TypeScript/issues/44366
Sign in to commenthttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/44366
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featurehttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Awaiting%20More%20Feedback%22
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.