René's URL Explorer Experiment


Title: `unknown`: less-permissive alternative to `any` · Issue #10715 · microsoft/TypeScript · GitHub

Open Graph Title: `unknown`: less-permissive alternative to `any` · Issue #10715 · microsoft/TypeScript

X Title: `unknown`: less-permissive alternative to `any` · Issue #10715 · microsoft/TypeScript

Description: The any type is more permissive than is desired in many circumstances. The problem is that any implicitly conforms to all possible interfaces; since no object actually conforms to every possible interface, any is implicitly type-unsafe. ...

Open Graph Description: The any type is more permissive than is desired in many circumstances. The problem is that any implicitly conforms to all possible interfaces; since no object actually conforms to every possible in...

X Description: The any type is more permissive than is desired in many circumstances. The problem is that any implicitly conforms to all possible interfaces; since no object actually conforms to every possible in...

Mail addresses
notifications@github.com
notifications@github.com

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

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`unknown`: less-permissive alternative to `any`","articleBody":"The `any` type is more permissive than is desired in many circumstances. The problem is that `any` implicitly conforms to all possible interfaces; since no object actually conforms to every possible interface, `any` is implicitly type-unsafe. Using `any` requires type-checking it manually; however, this checking is easy to forget or mess up. Ideally we'd want a type that conforms to `{}` but which can be refined to any interface via checking.\r\n\r\nI'll refer to this proposed type as `unknown`. The point of `unknown` is that it does not conform to any interface but refines to any interface. At the simplest, type casting can be used to convert `unknown` to any interface. All properties/indices on `unknown` are implicitly treated as `unknown` unless refined. \r\n\r\nThe `unknown` type becomes a good type to use for _untrusted_ data, e.g. data which _could_ match an interface but we aren't yet sure if it does. This is opposed to `any` which is good for _trusted_ data, e.g. data which could match an interface and we're comfortable assuming that to be true. Where `any` is the escape hatch out of the type system, `unknown` is the well-guarded and regulated entrance into the type system.\r\n\r\n(edit) Quick clarification: https://github.com/Microsoft/TypeScript/issues/10715#issuecomment-359551672\r\n\r\ne.g.,\r\n\r\n```\r\nlet a = JSON.parse(); // a is unknown\r\nif (Arary.isArray(a.foo)) ; // a is {foo: Array\u003cunknown>} extends unknown\r\nif (a.bar instanceof Bar) // a is {bar: Bar} extends unknown\r\nlet b = String(a.s); // b is string\r\na as MyInterface; // a implements MyInterface\r\n```\r\n\r\nVery roughly, `unknown` is equivalent to the pseudo-interface:\r\n\r\n```\r\npseudo_interface unknown extends null|undefined|Object {\r\n   [key: any]: unknown // this[?] is unknown\r\n   [any]: unknown // this.? is unknown\r\n   [key: any](): ()=>unknown // this.?() returns unknown\r\n}\r\n```\r\n\r\nI'm fairly certain that TypeScript's type model will need some rather large updates to handle the primary cases well, e.g. understanding that a type is freely refinable but not implicitly castable, or worse understanding that a type may have non-writeable properties and allowing refinement to being writeable (it probably makes a lot of sense to treat `unknown` as immutable at first).\r\n\r\nA use case is user-input from a file or Web service. There might well be an _expected_ interface, but we don't at first know that the data conforms. We currently have two options:\r\n\r\n1) Use the `any` type here. This is done with the `JSON.parse` return value for example. The compiler is totally  unable to help detect bugs where we pass the user data along without checking it first.\r\n2) Use the `Object` type here. This stops us from just passing the data along unknown, but getting it into the proper shape is somewhat cumbersome. Simple type casts fail because the compiler assumes any refinement is impossible.\r\n\r\nNeither of these is great. Here's a simplified example of a real bug:\r\n\r\n```\r\ninterface AccountData {\r\n  id: number;\r\n}\r\nfunction reifyAccount(data: AccountData);\r\n\r\nfunction readUserInput(): any;\r\n\r\nconst data = readUserInput(); // data is any\r\nreifyAccount(data); // oops, what if data doesn't have .id or it's not a number?\r\n```\r\n\r\nThe version using `Object` is cumbersome:\r\n\r\n```\r\nfunction readUserInput(): Object;\r\n\r\nconst data = readUserInput();\r\nreifyAccount(data); // compile error - GOOD!\r\nif (data as AccountData) // compile error - debatably good or cumbersome\r\n  reifyAccount(data);\r\nif (typeof data.id === 'number') // compile error - not helpful\r\n  reifyAccount(data as AccountInfo);\r\nif (typeof (data as any).id === 'number') // CUMBERSOME and error-prone\r\n  reifyAccount((data as any) as AccountInfo); // still CUMBERSOME and error-prone\r\n```\r\n\r\nWith the proposed `unknown` type;\r\n\r\n```\r\nfunction readUserInput(): unknown;\r\n\r\nconst data = readUserInput(); // data is unknown\r\nif (typeof data.id === 'number') // compiles - GOOD - refines data to {id: number} extends unknown\r\n  reifyAccount(data); // data matches {id: number} aka AccountData - SAFE\r\n```\r\n","author":{"url":"https://github.com/seanmiddleditch","@type":"Person","name":"seanmiddleditch"},"datePublished":"2016-09-05T23:25:10Z","url":"https://github.com/microsoft/TypeScript/issues/10715"}

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:96809c7e-8898-c6a1-22ea-22b3cc6ba642
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id9B3E:1CEA6F:B2E37D:EE025E:6B00ED39
html-safe-nonce0c55df3089c8d4c03ed50567f7674748a9e812ed279ea99cf143078a3cb920a2
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QjNFOjFDRUE2RjpCMkUzN0Q6RUUwMjVFOjZCMDBFRDM5IiwidmlzaXRvcl9pZCI6IjY3NDQ2ODcxODAyMzQ0MTk1MTMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmacea41546967a642bd5860a0a3d12cbb221959af6342398bb3c1674c3a2a0521f5
hovercard-subject-tagissue:175136144
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/10715/issue_layout
twitter:imagehttps://opengraph.githubassets.com/1367f291f77fc3e60dc9f13bed1e09d3f240f70307e3ef44bef9ef356f90ae8c/microsoft/TypeScript/issues/10715
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/1367f291f77fc3e60dc9f13bed1e09d3f240f70307e3ef44bef9ef356f90ae8c/microsoft/TypeScript/issues/10715
og:image:altThe any type is more permissive than is desired in many circumstances. The problem is that any implicitly conforms to all possible interfaces; since no object actually conforms to every possible in...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameseanmiddleditch
hostnamegithub.com
expected-hostnamegithub.com
None4ef498cbabcbe3c50befc7a5b743e7072ee770b4daf0e15e54801180d1fa91fc
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
release9983dd5da06943f833c971dd5b6bc988e2858be5
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/microsoft/TypeScript/issues/10715#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F10715
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%2F10715
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/10715
Reloadhttps://github.com/microsoft/TypeScript/issues/10715
Reloadhttps://github.com/microsoft/TypeScript/issues/10715
microsofthttps://github.com/microsoft
TypeScripthttps://github.com/microsoft/TypeScript
Codehttps://github.com/microsoft/TypeScript
Issues5k (5k)https://github.com/microsoft/TypeScript/issues
Pull requests308 (308)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/10715
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/10715
unknown: less-permissive alternative to anyhttps://github.com/microsoft/TypeScript/issues/10715#top
FixedA PR has been merged for this issuehttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Fixed%22
In DiscussionNot yet reached consensushttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22In%20Discussion%22
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%22
https://github.com/seanmiddleditch
Sean Middleditch (seanmiddleditch)https://github.com/seanmiddleditch
on Sep 5, 2016https://github.com/microsoft/TypeScript/issues/10715#issue-175136144
#10715 (comment)https://github.com/microsoft/TypeScript/issues/10715#issuecomment-359551672
Nexthttps://github.com/microsoft/TypeScript/issues/10715?timeline_page=1
Sean Middleditch (seanmiddleditch)https://github.com/seanmiddleditch
on Sep 5, 2016https://github.com/microsoft/TypeScript/issues/10715#event-778699711
Daniel Rosenwasser (DanielRosenwasser)https://github.com/DanielRosenwasser
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%22
In DiscussionNot yet reached consensushttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22In%20Discussion%22
on Sep 5, 2016https://github.com/microsoft/TypeScript/issues/10715#event-778707676
https://github.com/ahejlsberg
Anders Hejlsberg (ahejlsberg)https://github.com/ahejlsberg
on Sep 6, 2016https://github.com/microsoft/TypeScript/issues/10715#issuecomment-244824003
https://github.com/dead-claudia
Claudia Meadows (dead-claudia)https://github.com/dead-claudia
on Sep 6, 2016https://github.com/microsoft/TypeScript/issues/10715#issuecomment-244824747
https://github.com/yortus
Troy Gerwien (yortus)https://github.com/yortus
on Sep 6, 2016https://github.com/microsoft/TypeScript/issues/10715#issuecomment-244825896
#9999https://github.com/microsoft/TypeScript/issues/9999
Sean Middleditch (@seanmiddleditch)https://github.com/seanmiddleditch
https://github.com/dead-claudia
Claudia Meadows (dead-claudia)https://github.com/dead-claudia
on Sep 6, 2016https://github.com/microsoft/TypeScript/issues/10715#issuecomment-244827953
https://github.com/yortus
Troy Gerwien (yortus)https://github.com/yortus
on Sep 6, 2016https://github.com/microsoft/TypeScript/issues/10715#issuecomment-244839425
https://github.com/dead-claudia
Claudia Meadows (dead-claudia)https://github.com/dead-claudia
on Sep 6, 2016https://github.com/microsoft/TypeScript/issues/10715#issuecomment-244939248
https://github.com/isiahmeadowshttps://github.com/isiahmeadows
#10715 (comment)https://github.com/microsoft/TypeScript/issues/10715#issuecomment-244839425
https://github.com/notifications/unsubscribe-auth/AERrBC3YMJnttuqXk4Dq6iTh9VC7orY2ks5qnNg7gaJpZM4J1Wzbhttps://github.com/notifications/unsubscribe-auth/AERrBC3YMJnttuqXk4Dq6iTh9VC7orY2ks5qnNg7gaJpZM4J1Wzb
https://github.com/RyanCavanaugh
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
on Sep 28, 2016https://github.com/microsoft/TypeScript/issues/10715#issuecomment-250318449
https://github.com/saschanaz
Kagami Sascha Rosylight (saschanaz)https://github.com/saschanaz
on Sep 28, 2016https://github.com/microsoft/TypeScript/issues/10715#issuecomment-250327981
Ryan Cavanaugh (@RyanCavanaugh)https://github.com/RyanCavanaugh
https://github.com/dead-claudia
Claudia Meadows (dead-claudia)https://github.com/dead-claudia
on Sep 29, 2016https://github.com/microsoft/TypeScript/issues/10715#issuecomment-250531932
Kagami Sascha Rosylight (@saschanaz)https://github.com/saschanaz
Ryan Cavanaugh (@RyanCavanaugh)https://github.com/RyanCavanaugh
https://github.com/RyanCavanaughhttps://github.com/RyanCavanaugh
#10715 (comment)https://github.com/microsoft/TypeScript/issues/10715#issuecomment-250327981
https://github.com/notifications/unsubscribe-auth/AERrBO-yD4Qdc605NubmW6yKrBXH5ZFTks5quvQUgaJpZM4J1Wzbhttps://github.com/notifications/unsubscribe-auth/AERrBO-yD4Qdc605NubmW6yKrBXH5ZFTks5quvQUgaJpZM4J1Wzb
https://github.com/mhegazy
Mohamed Hegazy (mhegazy)https://github.com/mhegazy
on Sep 29, 2016https://github.com/microsoft/TypeScript/issues/10715#issuecomment-250533463
https://github.com/mihailik
mihailikhttps://github.com/mihailik
on Oct 26, 2016https://github.com/microsoft/TypeScript/issues/10715#issuecomment-256337526
https://github.com/saschanaz
Kagami Sascha Rosylight (saschanaz)https://github.com/saschanaz
on Oct 26, 2016https://github.com/microsoft/TypeScript/issues/10715#issuecomment-256349625
https://github.com/Conaclos
Victorien Elvinger (Conaclos)https://github.com/Conaclos
on Mar 9, 2018https://github.com/microsoft/TypeScript/issues/10715#issuecomment-371897088
Ryan Cavanaugh (@RyanCavanaugh)https://github.com/RyanCavanaugh
https://github.com/felipeochoa
Felipe (felipeochoa)https://github.com/felipeochoa
on Apr 1, 2018https://github.com/microsoft/TypeScript/issues/10715#issuecomment-377815963
#10485https://github.com/microsoft/TypeScript/issues/10485
#10485https://github.com/microsoft/TypeScript/issues/10485
https://github.com/SimonMeskens
Simon Meskens (SimonMeskens)https://github.com/SimonMeskens
on May 3, 2018https://github.com/microsoft/TypeScript/issues/10715#issuecomment-386169901
#23838https://github.com/microsoft/TypeScript/issues/23838
Victorien Elvinger (@Conaclos)https://github.com/Conaclos
https://github.com/pelotom
Tom Crockett (pelotom)https://github.com/pelotom
on May 3, 2018https://github.com/microsoft/TypeScript/issues/10715#issuecomment-386172063
https://github.com/dead-claudia
Claudia Meadows (dead-claudia)https://github.com/dead-claudia
on May 3, 2018https://github.com/microsoft/TypeScript/issues/10715#issuecomment-386187683
Simon Meskens (@SimonMeskens)https://github.com/SimonMeskens
https://github.com/jack-williams
Jack Williams (jack-williams)https://github.com/jack-williams
on May 4, 2018https://github.com/microsoft/TypeScript/issues/10715#issuecomment-386472317
https://github.com/pelotom
Tom Crockett (pelotom)https://github.com/pelotom
on May 4, 2018https://github.com/microsoft/TypeScript/issues/10715#issuecomment-386478139
Jack Williams (@jack-williams)https://github.com/jack-williams
https://github.com/simonbuchan
Simon Buchan (simonbuchan)https://github.com/simonbuchan
on May 4, 2018https://github.com/microsoft/TypeScript/issues/10715#issuecomment-386479881
https://github.com/pelotom
Tom Crockett (pelotom)https://github.com/pelotom
on May 4, 2018https://github.com/microsoft/TypeScript/issues/10715#issuecomment-386480284
https://github.com/jcready
James Wyatt Cready-Pyle (jcready)https://github.com/jcready
on May 4, 2018https://github.com/microsoft/TypeScript/issues/10715#issuecomment-386694000
Simon Buchan (@simonbuchan)https://github.com/simonbuchan
https://github.com/felixfbecker
Felix Becker (felixfbecker)https://github.com/felixfbecker
on May 4, 2018https://github.com/microsoft/TypeScript/issues/10715#issuecomment-386697877
Simon Buchan (@simonbuchan)https://github.com/simonbuchan
https://github.com/simonbuchan
Simon Buchan (simonbuchan)https://github.com/simonbuchan
on May 5, 2018https://github.com/microsoft/TypeScript/issues/10715#issuecomment-386769155
Anders Hejlsberg (ahejlsberg)https://github.com/ahejlsberg
completedhttps://github.com/microsoft/TypeScript/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
#24439https://github.com/microsoft/TypeScript/pull/24439
on May 30, 2018https://github.com/microsoft/TypeScript/issues/10715#event-1654133745
Anders Hejlsberg (ahejlsberg)https://github.com/ahejlsberg
FixedA PR has been merged for this issuehttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Fixed%22
on May 30, 2018https://github.com/microsoft/TypeScript/issues/10715#event-1654140024
microsofthttps://github.com/microsoft
on Jul 31, 2018https://github.com/microsoft/TypeScript/issues/10715#event-1763307824
Sign up for freehttps://github.com/signup?return_to=https://github.com/microsoft/TypeScript/issues/10715
Sign in to commenthttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/10715
FixedA PR has been merged for this issuehttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Fixed%22
In DiscussionNot yet reached consensushttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22In%20Discussion%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.