René's URL Explorer Experiment


Title: Problems with comparability between string literal types · Issue #6167 · microsoft/TypeScript · GitHub

Open Graph Title: Problems with comparability between string literal types · Issue #6167 · microsoft/TypeScript

X Title: Problems with comparability between string literal types · Issue #6167 · microsoft/TypeScript

Description: String literals currently only acquire a string literal type if they are contextually typed by another string literal or a union of string literals. They are then comparable and assertable to any string-like type (i.e. string, another st...

Open Graph Description: String literals currently only acquire a string literal type if they are contextually typed by another string literal or a union of string literals. They are then comparable and assertable to any s...

X Description: String literals currently only acquire a string literal type if they are contextually typed by another string literal or a union of string literals. They are then comparable and assertable to any s...

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

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Problems with comparability between string literal types","articleBody":"String literals currently only acquire a string literal type if they are contextually typed by another string literal or a union of string literals. They are then comparable and assertable to any string-like type (i.e. `string`, another string literal type, and any union with a string literal type inside of it).\n\nBy and large, this gives us the behavior you want; however, there are some problems with this approach.\n# Problems\n## `switch`/`case`\n\nThis is valid, which seems bad:\n\n``` ts\nlet x: \"foo\" = \"foo\";\n// ...\nswitch (x) {\n    case \"bar\":\n        console.log(\"wat\");\n}\n```\n\nWhen can `x` be `\"bar\"`? Without bypassing the type system, clearly never.\n## Equality\n\nThis is valid, which seems bad:\n\n``` ts\nlet x: \"foo\";\n// ...\nif (x === \"bar\") {\n}\n```\n\nThis is similar to the `switch` statement example. When will this condition be true? Again, without fooling the type system, it can't be.\n## Type assertions\n\nThese are both valid, which seems bad:\n\n``` ts\nlet x = \"hello\" as \"world\";\nlet y = \u003c\"bar\" | \"baz\">\"foo\";\n```\n\nThis is where we allow you to lie to the type system. This is probably way too lenient, so I'd say that this is bad.\n# Solutions?\n\nMost of the solutions here would require us to tighten our rules regarding allowing these operations between string-like entities. Essentially, we would need to capture some of the same behavior originally proposed in the pull request about a new \"comparable\" type relationship (#5517).\n## Widening\n\nWhy don't we have every string literal start out with a string literal type and widen to `string` when needed? We could certainly do that, but the question is when does a string literal type need to be widened?\n\nTo quote @JsonFreeman on https://github.com/Microsoft/TypeScript/issues/5300#issuecomment-150657208, widening as it would stand today would be a problem for type argument inference:\n\n> I think the biggest issue with the widening approach is that types are widened after type argument inference, which is an issue we have discussed before. It makes it so that any string literals that get inferred are automatically widened to string, which is a little unfortunate.\n\nHere's the problem Jason was talking about\n\n``` ts\ndeclare function f\u003cT>(x: T): T;\n\nlet x = f(\"hello\");\n```\n\nHere, we would always widen to `string` after picking a type for `T`. And even if we didn't, we'd still have other issues:\n\n``` ts\ndeclare function f\u003cT>(x: T, y: T): T;\n\nlet x = f(\"hello\", \"world\");\n```\n\nWhen we try to figure out what `T` should be, we'd have two different types: `\"hello\"` and `\"world`. Usually, when trying to infer `T` given the choice between something like `number` and `string`, we'll error. We could change this behavior, and while **it might be more desirable to infer `\"hello\" | \"world\"`** here, it would certainly be less consistent.\n# Contextually type `case` expressions\n\nFor the `switch`/`case` example, we could contextually type each `case` clause expression by the type of the `switch` expression:\n\n``` ts\nlet x: \"foo\" | \"bar;\n// ...\nswitch (x) {\n    case \"baz:\": // \u003c- error: type '\"baz\"' is not compatible with '\"foo\" | \"bar\"'\n        console.log(\"wat\");\n}\n```\n\nBut what about the other way around? We can't contextually type in both directions, because that would create a circularity. A `case` clause expression would try to grab a contextual type from the `switch` expression, which would try to grab the contextual type from its `case` clauses... etc.\n\nSo we could just make things unidirectional, which still has undesirable results\n\n``` ts\nlet x: \"foo\" | \"bar;\n// ...\nswitch (\"baz\") {\n    case x: // \u003c- okay: type '\"foo\" | \"bar\"' is compatible with 'string'\n        console.log(\"wat\");\n}\n```\n\nAnd admittedly, that code is less likely to be written, but really, what is fundamentally different about the first `switch (x)` example and the following?\n\n``` ts\nlet x: \"foo\" | \"bar\";\n// ...\nif (x === \"baz\") { // \u003c- okay: type '\"foo\" | \"bar\"' is compatible with 'string'\n    console.log(\"wat\");\n}\n```\n\nClearly you want an error here too, so it would be kind of weird to specially treat `switch` statements, but not equality comparisons.\n# Come up with a some new flow of information, like contextual typing\n\nFor equality checks, it'd be nice to have something that can flow both ways. For instance, in this example\n\n``` ts\nlet x: \"foo\" | \"bar\";\n// ...\nif (x === \"baz\") {\n    console.log(\"wat\");\n}\n```\n\nyou want `\"baz\"` to grab the type of `x`, **independent of this type relation**, and apply the same sort of information towards creating a string literal type as you would given a contextual type. Since `x` would have the type of `\"foo\" | \"bar\"`, which is a union containing string literal types, `\"baz\"` would acquire its string literal type for the purpose of this check.\n\nYou wouldn't end up catching the following\n\n``` ts\nif (\"hello\" === \"world\") {\n    // ...\n}\n```\n\nbecause each side would just have type `string`, and inform the other side with the type `string`. But you can already do that today, and this is a fairly silly case anyway, so it probably doesn't matter.\n\nThe biggest question is how we plan to implement this. It would certainly need a proposal.\n## Come up with some ad hoc checks\n\nWe could come up with some weak checks in these positions to figure out if the user is likely making an error. This would need a proposal.\n","author":{"url":"https://github.com/DanielRosenwasser","@type":"Person","name":"DanielRosenwasser"},"datePublished":"2015-12-19T00:58:11Z","url":"https://github.com/microsoft/TypeScript/issues/6167"}

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:a51262c5-dcbc-b2e9-33ae-3dfad741c0ab
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idE8FC:2C75BB:129C25:1911E2:6AFBDD83
html-safe-nonceebc4514c404cc5c3b816e464daf94f7cecc617dd88dac4fd723dda18b82b1366
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFOEZDOjJDNzVCQjoxMjlDMjU6MTkxMUUyOjZBRkJERDgzIiwidmlzaXRvcl9pZCI6IjQ3NzM2MDQzOTY0NzAwMTk4NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac28895a3dc4bd658edc9f5add1e550986904d808fd0b5851239b1742fdcf6dc94
hovercard-subject-tagissue:123050404
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/6167/issue_layout
twitter:imagehttps://opengraph.githubassets.com/3df590b4f09083b59fa039174c81492eda250b32cc03b05ca86f33c1e13ec9c3/microsoft/TypeScript/issues/6167
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/3df590b4f09083b59fa039174c81492eda250b32cc03b05ca86f33c1e13ec9c3/microsoft/TypeScript/issues/6167
og:image:altString literals currently only acquire a string literal type if they are contextually typed by another string literal or a union of string literals. They are then comparable and assertable to any s...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameDanielRosenwasser
hostnamegithub.com
expected-hostnamegithub.com
None744d4381b1c4dbd6d8736c7d7e5bbe7dc11217584e457a3555020c631764e8ec
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
releasedf859c48a4c8b4c0009c19bb4e82101aacb4b998
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/microsoft/TypeScript/issues/6167#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F6167
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%2F6167
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/6167
Reloadhttps://github.com/microsoft/TypeScript/issues/6167
Reloadhttps://github.com/microsoft/TypeScript/issues/6167
microsofthttps://github.com/microsoft
TypeScripthttps://github.com/microsoft/TypeScript
Codehttps://github.com/microsoft/TypeScript
Issues5k (5k)https://github.com/microsoft/TypeScript/issues
Pull requests334 (334)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/pulse
https://github.com/microsoft/TypeScript
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/6167
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/6167
Problems with comparability between string literal typeshttps://github.com/microsoft/TypeScript/issues/6167#top
Domain: Literal TypesUnit types including string literal types, numeric literal types, Boolean literals, null, undefinedhttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Domain%3A%20Literal%20Types%22
FixedA PR has been merged for this issuehttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Fixed%22
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%22
https://github.com/DanielRosenwasser
Daniel Rosenwasser (DanielRosenwasser)https://github.com/DanielRosenwasser
on Dec 19, 2015https://github.com/microsoft/TypeScript/issues/6167#issue-123050404
#5517https://github.com/microsoft/TypeScript/pull/5517
Jason Freeman (@JsonFreeman)https://github.com/JsonFreeman
#5300 (comment)https://github.com/microsoft/TypeScript/issues/5300#issuecomment-150657208
Daniel Rosenwasser (DanielRosenwasser)https://github.com/DanielRosenwasser
Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.https://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Needs%20Proposal%22
DiscussionIssues which may not have code impacthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Discussion%22
on Dec 19, 2015https://github.com/microsoft/TypeScript/issues/6167#event-496386370
https://github.com/RyanCavanaugh
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
on Dec 19, 2015https://github.com/microsoft/TypeScript/issues/6167#issuecomment-165944062
Aleksey-Bykovhttps://github.com/Aleksey-Bykov
https://github.com/JsonFreeman
Jason Freeman (JsonFreeman)https://github.com/JsonFreeman
on Dec 19, 2015https://github.com/microsoft/TypeScript/issues/6167#issuecomment-165955241
https://github.com/zpdDG4gta8XKpMCd
ZpdDG4gta (zpdDG4gta8XKpMCd)https://github.com/zpdDG4gta8XKpMCd
on Dec 19, 2015https://github.com/microsoft/TypeScript/issues/6167#issuecomment-165972053
https://github.com/falsandtru
falsandtru (falsandtru)https://github.com/falsandtru
on Dec 19, 2015https://github.com/microsoft/TypeScript/issues/6167#issuecomment-165978396
https://github.com/DanielRosenwasser
Daniel Rosenwasser (DanielRosenwasser)https://github.com/DanielRosenwasser
on Dec 20, 2015https://github.com/microsoft/TypeScript/issues/6167#issuecomment-166044642
Jason Freeman (@JsonFreeman)https://github.com/JsonFreeman
https://github.com/JsonFreeman
Jason Freeman (JsonFreeman)https://github.com/JsonFreeman
on Dec 20, 2015https://github.com/microsoft/TypeScript/issues/6167#issuecomment-166069579
https://github.com/JsonFreeman
Jason Freeman (JsonFreeman)https://github.com/JsonFreeman
on Dec 20, 2015https://github.com/microsoft/TypeScript/issues/6167#issuecomment-166070886
https://github.com/DanielRosenwasser
Daniel Rosenwasser (DanielRosenwasser)https://github.com/DanielRosenwasser
on Dec 20, 2015https://github.com/microsoft/TypeScript/issues/6167#issuecomment-166100416
https://github.com/JsonFreeman
Jason Freeman (JsonFreeman)https://github.com/JsonFreeman
on Dec 20, 2015https://github.com/microsoft/TypeScript/issues/6167#issuecomment-166164833
https://github.com/JsonFreeman
Jason Freeman (JsonFreeman)https://github.com/JsonFreeman
on Dec 21, 2015https://github.com/microsoft/TypeScript/issues/6167#issuecomment-166407153
Daniel Rosenwasser (DanielRosenwasser)https://github.com/DanielRosenwasser
Domain: Literal TypesUnit types including string literal types, numeric literal types, Boolean literals, null, undefinedhttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Domain%3A%20Literal%20Types%22
on Jan 12, 2016https://github.com/microsoft/TypeScript/issues/6167#event-512983166
https://github.com/DanielRosenwasser
Daniel Rosenwasser (DanielRosenwasser)https://github.com/DanielRosenwasser
on Jan 12, 2016https://github.com/microsoft/TypeScript/issues/6167#issuecomment-171074335
Anders Hejlsberg (@ahejlsberg)https://github.com/ahejlsberg
Ryan Cavanaugh (@RyanCavanaugh)https://github.com/RyanCavanaugh
Anders Hejlsberg (@ahejlsberg)https://github.com/ahejlsberg
https://github.com/JsonFreeman
Jason Freeman (JsonFreeman)https://github.com/JsonFreeman
on Jan 12, 2016https://github.com/microsoft/TypeScript/issues/6167#issuecomment-171084761
https://github.com/DanielRosenwasser
Daniel Rosenwasser (DanielRosenwasser)https://github.com/DanielRosenwasser
on Jan 12, 2016https://github.com/microsoft/TypeScript/issues/6167#issuecomment-171096320
https://github.com/DanielRosenwasser
Daniel Rosenwasser (DanielRosenwasser)https://github.com/DanielRosenwasser
on Jan 12, 2016https://github.com/microsoft/TypeScript/issues/6167#issuecomment-171100145
https://github.com/JsonFreeman
Jason Freeman (JsonFreeman)https://github.com/JsonFreeman
on Jan 13, 2016https://github.com/microsoft/TypeScript/issues/6167#issuecomment-171182577
https://github.com/DanielRosenwasser
Daniel Rosenwasser (DanielRosenwasser)https://github.com/DanielRosenwasser
on Jan 13, 2016https://github.com/microsoft/TypeScript/issues/6167#issuecomment-171455369
Jason Freeman (@JsonFreeman)https://github.com/JsonFreeman
https://github.com/JsonFreeman
Jason Freeman (JsonFreeman)https://github.com/JsonFreeman
on Jan 18, 2016https://github.com/microsoft/TypeScript/issues/6167#issuecomment-172426868
Mohamed Hegazy (mhegazy)https://github.com/mhegazy
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%22
DiscussionIssues which may not have code impacthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Discussion%22
on Feb 20, 2016https://github.com/microsoft/TypeScript/issues/6167#event-557400090
Daniel Rosenwasser (DanielRosenwasser)https://github.com/DanielRosenwasser
FixedA PR has been merged for this issuehttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Fixed%22
on Apr 1, 2016https://github.com/microsoft/TypeScript/issues/6167#event-611533056
https://github.com/DanielRosenwasser
Daniel Rosenwasser (DanielRosenwasser)https://github.com/DanielRosenwasser
on Apr 1, 2016https://github.com/microsoft/TypeScript/issues/6167#issuecomment-204471461
#5517https://github.com/microsoft/TypeScript/pull/5517
#7140https://github.com/microsoft/TypeScript/pull/7140
Daniel Rosenwasser (DanielRosenwasser)https://github.com/DanielRosenwasser
completedhttps://github.com/microsoft/TypeScript/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
on Apr 1, 2016https://github.com/microsoft/TypeScript/issues/6167#event-611533229
Daniel Rosenwasser (DanielRosenwasser)https://github.com/DanielRosenwasser
Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.https://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Needs%20Proposal%22
on May 8, 2016https://github.com/microsoft/TypeScript/issues/6167#event-653798329
microsofthttps://github.com/microsoft
on Jun 19, 2018https://github.com/microsoft/TypeScript/issues/6167#event-1689629604
Sign up for freehttps://github.com/signup?return_to=https://github.com/microsoft/TypeScript/issues/6167
Sign in to commenthttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/6167
Domain: Literal TypesUnit types including string literal types, numeric literal types, Boolean literals, null, undefinedhttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Domain%3A%20Literal%20Types%22
FixedA PR has been merged for this issuehttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Fixed%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.