Title: User-defined type assertions · Issue #17760 · microsoft/TypeScript · GitHub
Open Graph Title: User-defined type assertions · Issue #17760 · microsoft/TypeScript
X Title: User-defined type assertions · Issue #17760 · microsoft/TypeScript
Description: Proposal: support user-defined type assertions (keyword as) in addition to user-defined type guards (keyword is). A user-defined type assertion is a function with a return type expressed as in the following example (continuing the handbo...
Open Graph Description: Proposal: support user-defined type assertions (keyword as) in addition to user-defined type guards (keyword is). A user-defined type assertion is a function with a return type expressed as in the ...
X Description: Proposal: support user-defined type assertions (keyword as) in addition to user-defined type guards (keyword is). A user-defined type assertion is a function with a return type expressed as in the ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/17760
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"User-defined type assertions","articleBody":"Proposal: support user-defined type assertions (keyword `as`) in addition to user-defined type guards (keyword `is`).\r\n\r\nA user-defined type assertion is a function with a return type expressed as in the following example (continuing the handbook example in \"Advanced Types\").\r\n```ts\r\nfunction assertIsFish(pet: Fish | Bird): pet as Fish {\r\n if (!isFish(pet))\r\n throw new Error(\"birds are not supported\");\r\n}\r\n\r\nassertIsFish(pet);\r\n\r\npet.swim();\r\n```\r\n\r\n`:pet as fish` means that, if the function assertIsFish returns without throwing, pet is automatically asserted to be of type `Fish`.\r\n\r\nUse-cases:\r\n\r\n1. Validators: user-defined type assertions make it possible to encapsulate in functions the type narrowing that is currently available through control flow analysis. They also allow types that better describe the behavior of many existing libraries, including: node asserts, data validators that throw detailed errors, many test frameworks.\r\n\r\n2. Polyfills: enables describing functions that modify objects by adding new properties.\r\n```ts\r\naddName\u003cT>(obj: T): obj as T & { name: string } {\r\n obj.name = \"Mike\";\r\n}\r\n\r\nconst obj = {};\r\naddName(obj);\r\n\r\nobj.name.indexOf(\"i\");\r\n```\r\n\r\nValidator examples:\r\n\r\nsimple undefined checking\r\n```ts\r\nfunction notUndefined\u003cT>(obj: T | undefined): obj as T {\r\n if (obj === undefined)\r\n throw new Error(\"invalid object\");\r\n}\r\n\r\nconst myObj: string | undefined = {} as any;\r\n\r\n// [ts] Object is possibly 'undefined'.\r\nmyObj.indexOf(\"\");\r\n\r\nnotUndefined(myObj);\r\n\r\n// no errors\r\nmyObj.indexOf(\"\");\r\n```\r\n\r\n[joi-like](https://github.com/hapijs/joi) validation currently requires implementing the schema description of an object for runtime checking and copying the schema to an interface to have static type checking while the static type could be inferred by the implementation of the schema\r\n```ts\r\ninterface Schema\u003cT> {\r\n validate(obj: any): obj is T;\r\n}\r\n\r\nfunction attemptValidation\u003cT>(obj: any, schema: Schema\u003cT>): obj as T {\r\n if (!schema.validate(obj))\r\n throw new Error(\"very detailed object validation error\");\r\n}\r\n\r\n// complex schema implements T and T is inferred by the schema construction\r\n// this is already possible\r\nconst complexSchema = Joi.object({\r\n myField: Joi.date().iso()\r\n});\r\n\r\nattemptValidation(objFromTheRealWorld, complexSchema);\r\n\r\nconst day = objFromTheReadWorld.myField.getDay();\r\n```\r\n","author":{"url":"https://github.com/maghis","@type":"Person","name":"maghis"},"datePublished":"2017-08-12T15:45:43Z","url":"https://github.com/microsoft/TypeScript/issues/17760"}
| 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:e60f9d68-ef94-3975-33a8-fc0c4ba4719a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C31C:34CAB:31BD1BB:3FA5D29:6B01DD5A |
| html-safe-nonce | 95ad824006cc1df1c5f2ebe8df86aaf131092dd0b72a82b310693dce8f52d51b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMzFDOjM0Q0FCOjMxQkQxQkI6M0ZBNUQyOTo2QjAxREQ1QSIsInZpc2l0b3JfaWQiOiIxNzM4NzY5MDk4NzA5NTIzODAyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 31f3ee26e43a6a90207ba95aa0dd813cbeb75486ddc06c5c29668566ef64e844 |
| hovercard-subject-tag | issue:249819375 |
| 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/17760/issue_layout |
| twitter:image | https://opengraph.githubassets.com/35299c7d2abc9a7f3d40c16076488cb80d9e0446898d364dea24e8d5ba70a416/microsoft/TypeScript/issues/17760 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/35299c7d2abc9a7f3d40c16076488cb80d9e0446898d364dea24e8d5ba70a416/microsoft/TypeScript/issues/17760 |
| og:image:alt | Proposal: support user-defined type assertions (keyword as) in addition to user-defined type guards (keyword is). A user-defined type assertion is a function with a return type expressed as in the ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | maghis |
| 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