Title: Suggestion: add generic tagged objects to core library · Issue #10882 · microsoft/TypeScript · GitHub
Open Graph Title: Suggestion: add generic tagged objects to core library · Issue #10882 · microsoft/TypeScript
X Title: Suggestion: add generic tagged objects to core library · Issue #10882 · microsoft/TypeScript
Description: A lot of suggestions have been around nominal typing. A fair share of those are made by people who are pleasantly surprised to discover the tagged interface method. I propose adding Ryan Cavanaugh (@RyanCavanaugh)'s elegant generic solut...
Open Graph Description: A lot of suggestions have been around nominal typing. A fair share of those are made by people who are pleasantly surprised to discover the tagged interface method. I propose adding Ryan Cavanaugh ...
X Description: A lot of suggestions have been around nominal typing. A fair share of those are made by people who are pleasantly surprised to discover the tagged interface method. I propose adding Ryan Cavanaugh ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/10882
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Suggestion: add generic tagged objects to core library","articleBody":"A lot of suggestions have been around nominal typing. A fair share of those are made by people who are pleasantly surprised to discover the tagged interface method. I propose adding @RyanCavanaugh's [elegant generic solution](https://github.com/Microsoft/TypeScript/issues/202#issuecomment-246507970) to the library (and the documentation!), to help users discover this great feature. It would also avoid every popular Typescript library eventually adding their own version of this. I know I was surprised to find it and I would have loved to discover it sooner.\n\n``` typescript\ntype Branded\u003cT, U> = T & { [Symbol.species]: U };\n```\n\nExample use:\n\n``` typescript\n// UUID or a symbol, once symbol literal types are supported\ntype OrderUUID = \"d3a8e9b5-07c2-4a2d-a245-8a5908826b0f\";\n\n// Tagged Alias\ntype OrderID = Branded\u003cstring, OrderUUID>;\n```\n\nIt may be useful to split this into a type and an interface, though this may make it less elegant (and thus overreaching for inclusion in the core library):\n\n``` typescript\ninterface Specified\u003cT> {\n [Symbol.species]: T;\n}\n\ntype Branded\u003cT, U> = T & Specified\u003cU>;\n```\n\nIt does allow for more advanced uses of this system, such as runtime type checking:\n\n``` typescript\n// Is there a more elegant way to get a string literal type?\nconst OrderUUID = \"d3a8e9b5-07c2-4a2d-a245-8a5908826b0f\" as \"d3a8e9b5-07c2-4a2d-a245-8a5908826b0f\";\n\n// Tagged Class\nclass Order implements Specified\u003ctypeof OrderUUID> {\n [Symbol.species] = \"d3a8e9b5-07c2-4a2d-a245-8a5908826b0f\" as typeof OrderUUID;\n}\n\nfunction isOrder(arg: any): arg is Order {\n return OrderUUID in arg;\n}\n```\n\nThe naming might need some discussion.\n","author":{"url":"https://github.com/SimonMeskens","@type":"Person","name":"SimonMeskens"},"datePublished":"2016-09-12T23:16:39Z","url":"https://github.com/microsoft/TypeScript/issues/10882"}
| 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:27c58420-90ce-0701-164c-a9855af0890e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 87BA:4E3C:E79325:133F3D7:6B01022B |
| html-safe-nonce | 826349fd20dc2d3a81358aac66b8ef3c072ecbd3a0e27c02080424696998aa90 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4N0JBOjRFM0M6RTc5MzI1OjEzM0YzRDc6NkIwMTAyMkIiLCJ2aXNpdG9yX2lkIjoiODU2MDI5MDI1Mjc3Njg2NjM0NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | e73929f0ba510a141a069d33ccb86c86a43b2b563349e5ed002c6aea11c40ae1 |
| hovercard-subject-tag | issue:176508935 |
| 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/10882/issue_layout |
| twitter:image | https://opengraph.githubassets.com/07e635d15595c329952e2341b32981701dae4c0473412e542a2b33ec3c9ad9a1/microsoft/TypeScript/issues/10882 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/07e635d15595c329952e2341b32981701dae4c0473412e542a2b33ec3c9ad9a1/microsoft/TypeScript/issues/10882 |
| og:image:alt | A lot of suggestions have been around nominal typing. A fair share of those are made by people who are pleasantly surprised to discover the tagged interface method. I propose adding Ryan Cavanaugh ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | SimonMeskens |
| hostname | github.com |
| expected-hostname | github.com |
| None | cb7fa109eca7d4156c889f474306b0e91336055e9adf8fd4684b61e49094d124 |
| 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 | 2b59d31a4e44f08b7af67a4b67f8fd0af3befe2a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width