Title: Safe type assertion (upcast / widening) operator · Issue #56235 · microsoft/TypeScript · GitHub
Open Graph Title: Safe type assertion (upcast / widening) operator · Issue #56235 · microsoft/TypeScript
X Title: Safe type assertion (upcast / widening) operator · Issue #56235 · microsoft/TypeScript
Description: 🔍 Search Terms as, satisfies, type assertion, type casting ✅ Viability Checklist This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn't change the runtime behavior of existing JavaScript code This could b...
Open Graph Description: 🔍 Search Terms as, satisfies, type assertion, type casting ✅ Viability Checklist This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn't change the runtime behavior ...
X Description: 🔍 Search Terms as, satisfies, type assertion, type casting ✅ Viability Checklist This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn't change the runtime b...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/56235
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Safe type assertion (upcast / widening) operator","articleBody":"### 🔍 Search Terms\n\nas, satisfies, type assertion, type casting\n\n### ✅ Viability Checklist\n\n- [X] This wouldn't be a breaking change in existing TypeScript/JavaScript code\n- [X] This wouldn't change the runtime behavior of existing JavaScript code\n- [X] This could be implemented without emitting different JS based on the types of the expressions\n- [X] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)\n- [X] This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals\n\n### ⭐ Suggestion\n\nI suggest adding a safe type assertion operator, a kind of middle ground between current `as` and `satisfies`.\r\n\r\n- Like `as`, it should cast the expression to the asserted type\r\n- Like `satisfies`, it should only allow assertion if the asserted type is wider than the expression type\r\n\r\nHere's what it might look like:\r\n\r\n```ts\r\ntype Foo = { a: number }\r\n\r\nconst foo1 = { a: 1 } as! Foo // OK. foo1: Foo\r\n\r\nconst foo2 = { a: 1, b: 2 } as! Foo // OK, foo2: Foo\r\n\r\nconst foo3 = { b: 2 } as! Foo // Error, { b: number } is not assignable to Foo\r\n\r\nconst foo4 = { } as! Foo // Error, {} is not assignable to Foo\r\n```\r\n\n\n### 📃 Motivating Example\n\nFor example, such operator will allow to safely give named types to expressions without declaring extra variables or helper functions.\r\n\r\nConsider an async function `f` that returns an object of type `Foo`:\r\n\r\n```ts\r\ntype Foo = { a: number }\r\n\r\nconst f = async () => ({ a: 1 })\r\n```\r\n\r\nI want the returned object to strictly be of `Foo` type, how do I enforce this?\r\n\r\n1) Hard code the return type\r\n\r\n```ts\r\nconst f = async (): Promise\u003cFoo> => ({ a: 1 })\r\n```\r\n\r\nThe problem here is that I must write `Promise\u003cFoo>` instead of just `Foo`, but the `Promise` part can be inferred just fine. In real world these generics can be arbitrarily complex.\r\n\r\n2) Declare a variable\r\n\r\n```ts\r\nconst f = async () => {\r\n const result: Foo = { a: 1 }\r\n\r\n return result\r\n}\r\n```\r\n\r\nThis is better than retyping generics but still pretty verbose.\r\n\r\n3) Use a helper function\r\n\r\n```ts\r\nconst safeAssert = \u003cT>(value: T): T => value\r\n\r\nconst f = async () => safeAssert\u003cFoo>({ a: 1 })\r\n```\r\n\r\nThis is ok but still requires writing/importing helper functions.\r\n\r\n`as` is not an option here because it's unsafe, `satisfies` is neither because it doesn't name the type. Naming a type can be important for documentation purposes, this proposal allows to do this with less boilerplate.\r\n\r\n```ts\r\nconst f = async () => ({ a: 1}) as! Foo // f: () => Promise\u003cFoo>\r\n```\r\n\n\n### 💻 Use Cases\n\nAs described above, this feature can be used to safely cast expressions to specified types. A practical example when it can be useful is naming a type in an expression. Currently this is only covered by custom helper functions such as `\u003cT>(value: T): T => value`.\r\n","author":{"url":"https://github.com/bgenia","@type":"Person","name":"bgenia"},"datePublished":"2023-10-27T22:13:41Z","url":"https://github.com/microsoft/TypeScript/issues/56235"}
| 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:8b3f32dd-8f47-1b7f-8331-7531dba197ba |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A474:29FFA9:293558E:342FBCD:6B019E90 |
| html-safe-nonce | bde98dd9a907197f166dc8404ff8970af93b3ce0300ce6a0ad30ba86ed4c2206 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNDc0OjI5RkZBOToyOTM1NThFOjM0MkZCQ0Q6NkIwMTlFOTAiLCJ2aXNpdG9yX2lkIjoiNzkyODM5NTAxMzY5MTU1MjE2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | eda220697ec8d992abe338d353cc5531083de4bf7c2b4a3df2157ae30c9a3d9a |
| hovercard-subject-tag | issue:1966251776 |
| 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/56235/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f246a6354d55872739960da157026f92f3e552056939bf65f4ea7ed6916aba9e/microsoft/TypeScript/issues/56235 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f246a6354d55872739960da157026f92f3e552056939bf65f4ea7ed6916aba9e/microsoft/TypeScript/issues/56235 |
| og:image:alt | 🔍 Search Terms as, satisfies, type assertion, type casting ✅ Viability Checklist This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn't change the runtime behavior ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | bgenia |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| 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 | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width