Title: Feature: type alternatives (to facilitate traits/mixins/type decorators) · Issue #727 · microsoft/TypeScript · GitHub
Open Graph Title: Feature: type alternatives (to facilitate traits/mixins/type decorators) · Issue #727 · microsoft/TypeScript
X Title: Feature: type alternatives (to facilitate traits/mixins/type decorators) · Issue #727 · microsoft/TypeScript
Description: The type system does a great job at mirroring most type patterns in JS, all except mixins/traits, anything that decorates types - which unfortunately is very common in JS. Without it the type system remains incomplete - that is, we're st...
Open Graph Description: The type system does a great job at mirroring most type patterns in JS, all except mixins/traits, anything that decorates types - which unfortunately is very common in JS. Without it the type syste...
X Description: The type system does a great job at mirroring most type patterns in JS, all except mixins/traits, anything that decorates types - which unfortunately is very common in JS. Without it the type syste...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/727
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Feature: type alternatives (to facilitate traits/mixins/type decorators)","articleBody":"The type system does a great job at mirroring most type patterns in JS, all except mixins/traits, anything that decorates types - which unfortunately is very common in JS.\n\nWithout it the type system remains incomplete - that is, we're still one step short of being able to describe JavaScript mixin type patterns.\n\n[Duplication and interfaces](https://typescript.codeplex.com/wikipage?title=Mixins%20in%20TypeScript) are not the answer, because this does not reflect what's actually happening in JavaScript - e.g. not duplication, but type decoration of sorts. Not sure if I'm using the correct terminology here.\n\n[This small library](https://github.com/muut/riotjs/blob/master/lib/observable.js) is an example of a real-world decorator - duplicating all of it's method declarations in every single model type in a view-model hierarchy would be extremely impractical.\n\nThe following is a simplified example:\n\n```\nclass User {\n public name: string;\n}\n\ninterface Events {\n on(event: string, callback: {():void});\n // other methods here...\n}\n\nfunction addEvents\u003cT>(object: T): T {\n // decorate the object:\n object['on'] = function() {\n // ...\n }\n return \u003cT> {};\n}\n\nvar user = addEvents(new User());\n```\n\nThe drawback here is you have to choose which is more important - that `User` is still a `User` after being decorated by `addEvents()`, or that it is now also `Events`. There is no direct way to document this type pattern - your only option currently is to document the shape of the resulting type fully, duplicating all the member declarations contributed by the decorator.\n\nI propose syntax along the lines of the following, to support type alternatives:\n\n```\nfunction addEvents\u003cT>(object: T): T|Events {\n // decorate the object:\n object['on'] = function() {\n // ...\n }\n return \u003cT|Events> {};\n}\n\nvar user = addEvents(new User());\n```\n\nThe return type of the call is now `User|Events` which is a distinct composite type composed members of `User` and `Events`.\n\nThe resulting composite type extends `User` and implements `Events`, and should pass type-checks for both.\n\nThis should facilitate type-safe traits/mixins and probably other arbitrary type-patterns possible in JS.\n\nStatic type-hints with composite types are implemented in this way in e.g. PhpStorm using php-doc annotations, and it works well.\n\nI think this may be a simpler and more flexible way to support mixins/traits, without explicitly adding support for those patterns specifically.\n\nThoughts?\n","author":{"url":"https://github.com/mindplay-dk","@type":"Person","name":"mindplay-dk"},"datePublished":"2014-09-23T11:58:55Z","url":"https://github.com/microsoft/TypeScript/issues/727"}
| 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:4d6c1b52-50b2-9e9c-9ffe-5486eba01f7f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C542:24C546:39E998:4DE597:6AFBE9C6 |
| html-safe-nonce | 9429c72d0b14a33df88f7193702afe0aaf9d1479f77bbce850df83c688dc8bd0 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNTQyOjI0QzU0NjozOUU5OTg6NERFNTk3OjZBRkJFOUM2IiwidmlzaXRvcl9pZCI6IjMxNzE1MzQ2MDA1NTU3MTkxMTAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 9ef4d18c105dba88845fd79c89b2ca8b8521f6573160cd831e6dbbab1868c072 |
| hovercard-subject-tag | issue:43620396 |
| 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/727/issue_layout |
| twitter:image | https://opengraph.githubassets.com/67ffc8a9c2edf09ea371f293cacda88da269f328c4c0fc09d57579b8176b0e1c/microsoft/TypeScript/issues/727 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/67ffc8a9c2edf09ea371f293cacda88da269f328c4c0fc09d57579b8176b0e1c/microsoft/TypeScript/issues/727 |
| og:image:alt | The type system does a great job at mirroring most type patterns in JS, all except mixins/traits, anything that decorates types - which unfortunately is very common in JS. Without it the type syste... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mindplay-dk |
| hostname | github.com |
| expected-hostname | github.com |
| None | 744d4381b1c4dbd6d8736c7d7e5bbe7dc11217584e457a3555020c631764e8ec |
| 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 | df859c48a4c8b4c0009c19bb4e82101aacb4b998 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width