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
Domain: github.com
{"@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-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:96809c7e-8898-c6a1-22ea-22b3cc6ba642 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9B3E:1CEA6F:B2E37D:EE025E:6B00ED39 |
| html-safe-nonce | 0c55df3089c8d4c03ed50567f7674748a9e812ed279ea99cf143078a3cb920a2 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QjNFOjFDRUE2RjpCMkUzN0Q6RUUwMjVFOjZCMDBFRDM5IiwidmlzaXRvcl9pZCI6IjY3NDQ2ODcxODAyMzQ0MTk1MTMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | ea41546967a642bd5860a0a3d12cbb221959af6342398bb3c1674c3a2a0521f5 |
| hovercard-subject-tag | issue:175136144 |
| 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/10715/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1367f291f77fc3e60dc9f13bed1e09d3f240f70307e3ef44bef9ef356f90ae8c/microsoft/TypeScript/issues/10715 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1367f291f77fc3e60dc9f13bed1e09d3f240f70307e3ef44bef9ef356f90ae8c/microsoft/TypeScript/issues/10715 |
| og:image:alt | 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... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | seanmiddleditch |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4ef498cbabcbe3c50befc7a5b743e7072ee770b4daf0e15e54801180d1fa91fc |
| 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 | 9983dd5da06943f833c971dd5b6bc988e2858be5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width