René's URL Explorer Experiment


Title: Require all user-defined guards to accept type 'any' · Issue #4898 · microsoft/TypeScript · GitHub

Open Graph Title: Require all user-defined guards to accept type 'any' · Issue #4898 · microsoft/TypeScript

X Title: Require all user-defined guards to accept type 'any' · Issue #4898 · microsoft/TypeScript

Description: If a guard accepts an input type other than any, it cannot be assured to provide any meaningful or reliable result: interface A { func1: () => number; } interface B extends A { func2: () => string; } function isB(x: A): x is B { return x...

Open Graph Description: If a guard accepts an input type other than any, it cannot be assured to provide any meaningful or reliable result: interface A { func1: () => number; } interface B extends A { func2: () => string;...

X Description: If a guard accepts an input type other than any, it cannot be assured to provide any meaningful or reliable result: interface A { func1: () => number; } interface B extends A { func2: () => s...

Opengraph URL: https://github.com/microsoft/TypeScript/issues/4898

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Require all user-defined guards to accept type 'any'","articleBody":"If a guard accepts an input type other than `any`, it cannot be assured to provide any meaningful or reliable result:\n\n``` ts\ninterface A { \n    func1: () => number;\n}\n\ninterface B extends A {\n    func2: () => string;\n}\n\nfunction isB(x: A): x is B {\n    return x != null && typeof x.func2 === \"function\";\n}\n\nfunction example(input: A) {\n    if (isB(input)) {\n        input.func2(); // Guaranteed to work!\n        input.func1(); // Can fail :(\n    }\n}\n```\n\nThe problem is that the guard `isB` _assumed_ `input` had type `A`, but that assumption is not generally safe to make, especially when the function was called by a plain Javascript consumer, who does not have a compile-time checking mechanism for the associated static type. To fix this, we need to define an additional guard for `A`, that would accept input type `any`.\n\n``` ts\nfunction isA(x: any): x is A {\n    return x != null && typeof x === \"object\" && typeof x.func1 === \"function\";\n}\n```\n\nand then combine it with the check for `isB`:\n\n``` ts\nfunction example(input: A) {\n    if (isA(input) && isB(input)) {\n        input.func2(); // Guaranteed to work!\n        input.func1(); // Guaranteed to work!\n    }\n}\n```\n\nBut now `isB` is _dependent_ on a prior call to `isA` to provide a correct result, meaning it still isn't reliable or useful by itself. However, we can fix this by having `isB` internally call `isA`, and now its input type can also be safely changed to `any`:\n\n``` ts\nfunction isB(x: any): x is B {\n    return isA(x) && typeof x.func2 === \"function\";\n}\n```\n\nThis transformation can be done with any user-defined guard that accepts a type other than `any` and removes the need for these types of guards in general.\n## Class embedded guards\n\nGuards that are embedded in classes:\n\n``` ts\nclass A {\n    ...\n\n    isB(): this is B {\n        return false;\n    }\n}\n\nclass B extends A {\n    ...\n\n    isB(): this is B {\n        return true;\n    }\n}\n\nclass C extends B {\n    ...\n\n    isB(): this is B {\n        return false;\n    }\n}\n```\n\nAre even less reliable:\n\n``` ts\nfunction example(input: A) {\n    if (input.isB()) { \n        ...\n    }\n}\n\nexample(undefined); // Error, 'input' is 'undefined'\nexample(callSomeUnsafeFunction()); // Error, 'isB' method is not found\n```\n\nand would always need to be composed with a check for the base type (which should hopefully also include a check for `null` or `undefined` inputs):\n\n``` ts\nfunction example(x: A) {\n    if (isA(x) && x.isB()) {\n        ...\n    }\n}\n```\n\nTo fix this, we'll create a function that combines all the tests for `B` and accepts type `any`:\n\n``` ts\n// This guard would only return true if the input is a strict instance of 'B',\n// and not one of the derived classes.\nfunction isStrictlyB(x: any): x is B {\n    return x != null && isA(x) && isB(x) && !isC(X);\n}\n```\n\nThis removes the need for these types of guards as well.\n## Implications\n- All user-defined guards can now assumed to be sound at _run-time_ when called at any position and context and to correctly handle `null` and `undefined` input values.\n- All user-defined guards can now be safely applied on function arguments, including when the function is called by plain Javascript consumers.\n- All user-defined guards can be safely used with automated run-time type checkers without needing to apply complex search algorithms in attempt to assemble logically sound compositions based on multiple guards.\n- Programmers are encouraged to apply healthy and sound practices when writing run-time checks.\n- Type-safety is improved in general.\n## Conclusion\n\nGuards that apply compile-time assumptions on their input value's type are not generally reliable for use by themselves, may yield incorrect results or throw an exception. There are safer and more reusable alternatives for these types of guards, thus they can be deprecated without a significant loss of functionality.\n","author":{"url":"https://github.com/rotemdan","@type":"Person","name":"rotemdan"},"datePublished":"2015-09-21T14:38:48Z","url":"https://github.com/microsoft/TypeScript/issues/4898"}

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:6e487b04-85fe-895f-3ea5-d3c8a31ed309
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idA676:363D8E:2BAA33:398071:6B00C175
html-safe-nonce5d0e452627cc91678d2220dc761ff4fb945a2f2b871eeb0cc9b1a42251e11d40
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNjc2OjM2M0Q4RToyQkFBMzM6Mzk4MDcxOjZCMDBDMTc1IiwidmlzaXRvcl9pZCI6IjE1OTQ5NDc5NTY5Nzk1ODEzMDEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmaccc9db9acb37bb8b75756be0762cb412326d490661e624bb865c3abf0cad77df0
hovercard-subject-tagissue:107527074
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/4898/issue_layout
twitter:imagehttps://opengraph.githubassets.com/3c772a22c0b34e797f86ed22f99d144607059a6fa254bad0088e0f81eb8ac0ad/microsoft/TypeScript/issues/4898
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/3c772a22c0b34e797f86ed22f99d144607059a6fa254bad0088e0f81eb8ac0ad/microsoft/TypeScript/issues/4898
og:image:altIf a guard accepts an input type other than any, it cannot be assured to provide any meaningful or reliable result: interface A { func1: () => number; } interface B extends A { func2: () => string;...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamerotemdan
hostnamegithub.com
expected-hostnamegithub.com
None93edd3bebc4afb7ce711ee74cb5d2a95927850b082a4d43e212222389cb1c415
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
release1fee81a280ea86312b69448e261b418189bfd5b5
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/microsoft/TypeScript/issues/4898#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F4898
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%2F4898
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/4898
Reloadhttps://github.com/microsoft/TypeScript/issues/4898
Reloadhttps://github.com/microsoft/TypeScript/issues/4898
microsofthttps://github.com/microsoft
TypeScripthttps://github.com/microsoft/TypeScript
Codehttps://github.com/microsoft/TypeScript
Issues5k (5k)https://github.com/microsoft/TypeScript/issues
Pull requests307 (307)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/4898
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/4898
Require all user-defined guards to accept type 'any'https://github.com/microsoft/TypeScript/issues/4898#top
DeclinedThe issue was declined as something which matches the TypeScript visionhttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Declined%22
Out of ScopeThis idea sits outside of the TypeScript language design constraintshttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Out%20of%20Scope%22
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%22
https://github.com/rotemdan
Rotem Dan (rotemdan)https://github.com/rotemdan
on Sep 21, 2015https://github.com/microsoft/TypeScript/issues/4898#issue-107527074
Mohamed Hegazy (mhegazy)https://github.com/mhegazy
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%22
DeclinedThe issue was declined as something which matches the TypeScript visionhttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Declined%22
on Sep 21, 2015https://github.com/microsoft/TypeScript/issues/4898#event-415142415
https://github.com/mhegazy
Mohamed Hegazy (mhegazy)https://github.com/mhegazy
on Sep 21, 2015https://github.com/microsoft/TypeScript/issues/4898#issuecomment-142100937
Mohamed Hegazy (mhegazy)https://github.com/mhegazy
completedhttps://github.com/microsoft/TypeScript/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
on Sep 21, 2015https://github.com/microsoft/TypeScript/issues/4898#event-415200106
Mohamed Hegazy (mhegazy)https://github.com/mhegazy
Out of ScopeThis idea sits outside of the TypeScript language design constraintshttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Out%20of%20Scope%22
on Sep 21, 2015https://github.com/microsoft/TypeScript/issues/4898#event-415200182
https://github.com/rotemdan
Rotem Dan (rotemdan)https://github.com/rotemdan
on Sep 21, 2015https://github.com/microsoft/TypeScript/issues/4898#issuecomment-142108486
Mohamed Hegazy (@mhegazy)https://github.com/mhegazy
Ryan Cavanaugh (@RyanCavanaugh)https://github.com/RyanCavanaugh
Ryan Cavanaugh (@RyanCavanaugh)https://github.com/RyanCavanaugh
Ryan Cavanaugh (@RyanCavanaugh)https://github.com/RyanCavanaugh
Ryan Cavanaugh (@RyanCavanaugh)https://github.com/RyanCavanaugh
Ryan Cavanaugh (@RyanCavanaugh)https://github.com/RyanCavanaugh
https://github.com/RyanCavanaugh
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
on Sep 21, 2015https://github.com/microsoft/TypeScript/issues/4898#issuecomment-142123350
#4868https://github.com/microsoft/TypeScript/issues/4868
https://github.com/rotemdan
Rotem Dan (rotemdan)https://github.com/rotemdan
on Sep 21, 2015https://github.com/microsoft/TypeScript/issues/4898#issuecomment-142134866
Ryan Cavanaugh (@RyanCavanaugh)https://github.com/RyanCavanaugh
https://github.com/RyanCavanaugh
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
on Sep 21, 2015https://github.com/microsoft/TypeScript/issues/4898#issuecomment-142137712
https://github.com/rotemdan
Rotem Dan (rotemdan)https://github.com/rotemdan
on Sep 21, 2015https://github.com/microsoft/TypeScript/issues/4898#issuecomment-142141971
Ryan Cavanaugh (@RyanCavanaugh)https://github.com/RyanCavanaugh
https://github.com/danquirk
Dan Quirk (danquirk)https://github.com/danquirk
on Sep 22, 2015https://github.com/microsoft/TypeScript/issues/4898#issuecomment-142145294
https://github.com/rotemdan
Rotem Dan (rotemdan)https://github.com/rotemdan
on Sep 22, 2015https://github.com/microsoft/TypeScript/issues/4898#issuecomment-142199125
Dan Quirk (@danquirk)https://github.com/danquirk
Ryan Cavanaugh (@RyanCavanaugh)https://github.com/RyanCavanaugh
Mohamed Hegazy (@mhegazy)https://github.com/mhegazy
https://github.com/danquirk
Dan Quirk (danquirk)https://github.com/danquirk
on Sep 22, 2015https://github.com/microsoft/TypeScript/issues/4898#issuecomment-142372918
https://github.com/rotemdan
Rotem Dan (rotemdan)https://github.com/rotemdan
on Sep 22, 2015https://github.com/microsoft/TypeScript/issues/4898#issuecomment-142383210
Dan Quirk (@danquirk)https://github.com/danquirk
https://github.com/danquirk
Dan Quirk (danquirk)https://github.com/danquirk
on Sep 22, 2015https://github.com/microsoft/TypeScript/issues/4898#issuecomment-142388815
https://github.com/RyanCavanaugh
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
on Sep 22, 2015https://github.com/microsoft/TypeScript/issues/4898#issuecomment-142391359
All changes start at -100http://blogs.msdn.com/b/ericgu/archive/2004/01/12/57985.aspx
https://github.com/rotemdan
Rotem Dan (rotemdan)https://github.com/rotemdan
on Sep 22, 2015https://github.com/microsoft/TypeScript/issues/4898#issuecomment-142431653
Ryan Cavanaugh (@RyanCavanaugh)https://github.com/RyanCavanaugh
this commenthttps://github.com/Microsoft/TypeScript/issues/4898#issuecomment-142199125
https://github.com/mhegazy
Mohamed Hegazy (mhegazy)https://github.com/mhegazy
on Sep 22, 2015https://github.com/microsoft/TypeScript/issues/4898#issuecomment-142433587
Rotem Dan (@rotemdan)https://github.com/rotemdan
microsofthttps://github.com/microsoft
on Jun 19, 2018https://github.com/microsoft/TypeScript/issues/4898#event-1689616374
Sign up for freehttps://github.com/signup?return_to=https://github.com/microsoft/TypeScript/issues/4898
Sign in to commenthttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/4898
DeclinedThe issue was declined as something which matches the TypeScript visionhttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Declined%22
Out of ScopeThis idea sits outside of the TypeScript language design constraintshttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Out%20of%20Scope%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.