Title: feature request: support for mixins composed from other mixins. · Issue #32080 · microsoft/TypeScript · GitHub
Open Graph Title: feature request: support for mixins composed from other mixins. · Issue #32080 · microsoft/TypeScript
X Title: feature request: support for mixins composed from other mixins. · Issue #32080 · microsoft/TypeScript
Description: Search Terms Suggestion At the moment, it seems to be very difficult to compose mixins from other mixins. Here's an example on StackOverflow: https://stackoverflow.com/questions/56680049 Here's an example on playground. The code: type Co...
Open Graph Description: Search Terms Suggestion At the moment, it seems to be very difficult to compose mixins from other mixins. Here's an example on StackOverflow: https://stackoverflow.com/questions/56680049 Here's an ...
X Description: Search Terms Suggestion At the moment, it seems to be very difficult to compose mixins from other mixins. Here's an example on StackOverflow: https://stackoverflow.com/questions/56680049 Here...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/32080
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"feature request: support for mixins composed from other mixins.","articleBody":"## Search Terms\r\n\r\n\r\n## Suggestion\r\n\r\nAt the moment, it seems to be very difficult to compose mixins from other mixins.\r\n\r\nHere's an example on StackOverflow: https://stackoverflow.com/questions/56680049\r\n\r\nHere's an [example on playground](https://www.typescriptlang.org/play/#code/C4TwDgpgBAwg9gOwM7AE4FcDGw6oDwAqUAvFAIYIgA0UAglBAB7AQIAmS5lA2gLolcQfAHwCEEAO5QAFADp5ZAFx0AlCVEEAsACgdAM3QJsAS0RQAYnDgBZY42MJCDZqw6xEKDNlzDpAITIkCGUCNQBvKB0dKBioVAhgdFQEKEwAG0DOSzhnFnZOAKCoCOjYsr0rAQByCrgq0piAXyjtZt1tAyNgUxSA1Ft7RyImPLd4ZDQsHFRfQuCoUOLI9rL4xOTUjKQCslRc1yyrAYd-QIhw5bKylhRpC4arsswPODSIWTS4AHNpYAALYxIWS1NQAelBMQAEgBRABK0Jo-0BwMqgKgVQoICqJGkDzKbXxLUaQA).\r\n\r\nThe code:\r\n\r\n```ts\r\ntype Constructor\u003cT = any, A extends any[] = any[]> = new (...a: A) => T\r\n\r\nfunction FooMixin\u003cT extends Constructor>(Base: T) { \r\n return class Foo extends Base { \r\n foo = 'foo'\r\n }\r\n}\r\n\r\nfunction BarMixin\u003cT extends Constructor>(Base: T) { \r\n return class Bar extends FooMixin(Base) { \r\n test() { \r\n console.log(this.foo) // PROBLEM: this.foo is 'any' =(\r\n }\r\n }\r\n}\r\n```\r\n\r\n## Use Cases\r\n\r\nTo make it simpler to make mixins (and compose them) like we can in plain JavaScript.\r\n\r\nI'm porting JavaScript code to TypeScript, and the JavaScript makes great use of mixins (including composing new mixins from other mixins), but the composition ispractically impossible to do in TypeScript without very tedious type casting.\r\n\r\n## Examples\r\n\r\nHere is the plain JS version of the above example:\r\n\r\n```js\r\nfunction FooMixin(Base) { \r\n return class Foo extends Base { \r\n foo = 'foo'\r\n }\r\n}\r\n\r\nfunction BarMixin(Base) { \r\n // BarMixin is composed with FooMixin\r\n return class Bar extends FooMixin(Base) { \r\n test() { \r\n console.log(this.foo) // this.foo is obviously inherited from FooMixin!\r\n // ^--- This shoud not be an error!\r\n }\r\n }\r\n}\r\n```\r\n\r\nIt seems to me, that the type checker can realize that the class returned from `FooMixin(Base)` will be a `typeof Foo`. The type system could at least be able to allow the `Bar` class to use methods and properties from `Foo`, despite not knowing what the `Base` class will be.\r\n\r\nYou can also imagine this problem gets worse with more composition, f.e.\r\n\r\n```js\r\n return class Bar extends Foo(Baz(Lorem(Ipsum(Base)))) {\r\n```\r\n\r\nIt should also be possible to constrain the constructor to inherit from a certain base class. For example, the following doesn't work:\r\n\r\n(EDIT: this part may actually be moved to a separate issue)\r\n(EDIT 2: this part seems to be resolved)\r\n\r\n```ts\r\n// Think about Custom Elements here:\r\nfunction FooMixin\u003cT extends typeof HTMLElement>(Base: T) { \r\n return class Foo extends Base { \r\n test() {\r\n this.setAttribute('foo', 'bar')\r\n }\r\n }\r\n}\r\n```\r\n\r\n[playground link](http://www.typescriptlang.org/play/#src=function%20FooMixin%3CT%20extends%20typeof%20HTMLElement%3E(Base%3A%20T)%20%7B%20%0D%0A%0D%0A%20%20%20%20return%20class%20Foo%20extends%20Base%20%7B%20%0D%0A%20%20%20%20%20%20%20%20test()%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20this.setAttribute('foo'%2C%20'bar')%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%7D%0D%0A%0D%0A%7D)\r\n\r\nAs @dragomirtitian pointed out on SO, there are workarounds, but they appear to be very complicated and impractical.\r\n\r\nHere's a more realistic example of what I'm doing in JS (and trying to port to TS): I'm using a `Mixin()` helper function, as a type declaration for the following example, which in practice implements things like `Symbol.hasInstance` to check if instances are `instanceof` a given mixin, prevents duplicate mixin applications, and other features, but the types don't work:\r\n\r\n```ts\r\ntype Constructor\u003cT = any, A extends any[] = any[]> = new (...a: A) => T\r\n\r\ntype MixinFunction = \u003cTSuper>(baseClass: Constructor\u003cTSuper>) => Constructor\u003cTSuper>\r\n\r\n// this function does awesome: ensures mixins aren't applied\r\n// more than once on a prototype chain, sets up Symbol.hasInstance so that\r\n// instanceof checks works with any mixin application, etc.\r\ndeclare function Mixin\u003cT extends MixinFunction>(\r\n mixinFn: T,\r\n DefaultBase?: Constructor\r\n): ReturnType\u003cT> & {mixin: T}\r\n\r\nfunction FooMixin\u003cT extends Constructor>(Base: T) { \r\n return class Foo extends Base { \r\n foo = 'foo'\r\n }\r\n}\r\n\r\nconst Foo = Mixin(FooMixin)\r\ntype Foo = typeof Foo\r\n\r\n\r\nfunction BarMixin\u003cT extends Constructor>(Base: T) { \r\n return class Bar extends Foo.mixin(Base) {\r\n bar = 'bar'\r\n\r\n test() {\r\n this.foo = 'foofoo' // should work!\r\n }\r\n }\r\n}\r\n\r\nconst Bar = Mixin(BarMixin)\r\n\r\nclass Baz extends Bar {\r\n\r\n test() {\r\n this.bar = 'barbar' // should work!\r\n this.foo = 'foofoo' // should work!\r\n }\r\n\r\n}\r\n\r\nconst f: Foo = new Bar()\r\n```\r\n\r\n[playground link](http://www.typescriptlang.org/play/#src=type%20Constructor%3CT%20%3D%20any%2C%20A%20extends%20any%5B%5D%20%3D%20any%5B%5D%3E%20%3D%20new%20(...a%3A%20A)%20%3D%3E%20T%0D%0A%0D%0Atype%20MixinFunction%20%3D%20%3CTSuper%3E(baseClass%3A%20Constructor%3CTSuper%3E)%20%3D%3E%20Constructor%3CTSuper%3E%0D%0A%0D%0A%2F%2F%20this%20function%20does%20awesome%3A%20ensures%20mixins%20aren't%20applied%0D%0A%2F%2F%20more%20than%20once%20on%20a%20prototype%20chain%2C%20sets%20up%20Symbol.hasInstance%20so%20that%0D%0A%2F%2F%20instanceof%20checks%20works%20with%20any%20mixin%20application%2C%20etc.%0D%0Adeclare%20function%20Mixin%3CT%20extends%20MixinFunction%3E(%0D%0A%20%20%20%20mixinFn%3A%20T%2C%0D%0A%20%20%20%20DefaultBase%3F%3A%20Constructor%0D%0A)%3A%20ReturnType%3CT%3E%20%26%20%7Bmixin%3A%20T%7D%0D%0A%0D%0Afunction%20FooMixin%3CT%20extends%20Constructor%3E(Base%3A%20T)%20%7B%20%0D%0A%20%20%20%20return%20class%20Foo%20extends%20Base%20%7B%20%0D%0A%20%20%20%20%20%20%20%20foo%20%3D%20'foo'%0D%0A%20%20%20%20%7D%0D%0A%7D%0D%0A%0D%0Aconst%20Foo%20%3D%20Mixin(FooMixin)%0D%0Atype%20Foo%20%3D%20typeof%20Foo%0D%0A%0D%0A%0D%0Afunction%20BarMixin%3CT%20extends%20Constructor%3E(Base%3A%20T)%20%7B%20%0D%0A%20%20%20%20return%20class%20Bar%20extends%20Foo.mixin(Base)%20%7B%0D%0A%20%20%20%20%20%20%20%20bar%20%3D%20'bar'%0D%0A%0D%0A%20%20%20%20%20%20%20%20test()%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20this.foo%20%3D%20'foofoo'%20%2F%2F%20should%20work!%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%7D%0D%0A%7D%0D%0A%0D%0Aconst%20Bar%20%3D%20Mixin(BarMixin)%0D%0A%0D%0Aclass%20Baz%20extends%20Bar%20%7B%0D%0A%0D%0A%20%20%20%20test()%20%7B%0D%0A%20%20%20%20%20%20%20%20this.bar%20%3D%20'barbar'%20%2F%2F%20should%20work!%0D%0A%20%20%20%20%20%20%20%20this.foo%20%3D%20'foofoo'%20%2F%2F%20should%20work!%0D%0A%20%20%20%20%7D%0D%0A%0D%0A%7D%0D%0A%0D%0Aconst%20f%3A%20Foo%20%3D%20new%20Bar())\r\n\r\nIs there a way to do this currently, that we may have missed? (cc: @justinfagnani)\r\n\r\n## Checklist\r\n\r\nMy suggestion meets these guidelines:\r\n\r\n* [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code\r\n* [x] This wouldn't change the runtime behavior of existing JavaScript code\r\n* [x] This could be implemented without emitting different JS based on the types of the expressions\r\n* [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)\r\n* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).\r\n\r\n","author":{"url":"https://github.com/trusktr","@type":"Person","name":"trusktr"},"datePublished":"2019-06-25T03:27:25Z","url":"https://github.com/microsoft/TypeScript/issues/32080"}
| 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:20d66509-4a8b-2e27-f7f3-2ebc43b77dfe |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D582:29AEE8:280BEDC:355E7CE:6B01B09B |
| html-safe-nonce | 63dd7e41421abd5e2a6e7a8af1d020bb280bc7819677a3d5dce99f71f2381057 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENTgyOjI5QUVFODoyODBCRURDOjM1NUU3Q0U6NkIwMUIwOUIiLCJ2aXNpdG9yX2lkIjoiMzEzMjExOTAwMjc4MDkwNTYyNyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 6e561facf84167e268772c4d7346082d301e72acaf53221fece56e621868f289 |
| hovercard-subject-tag | issue:460189894 |
| 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/32080/issue_layout |
| twitter:image | https://opengraph.githubassets.com/229738c74ce47dcfbbe797fccca854f76382e9c81937d709db82ffac87cb95d0/microsoft/TypeScript/issues/32080 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/229738c74ce47dcfbbe797fccca854f76382e9c81937d709db82ffac87cb95d0/microsoft/TypeScript/issues/32080 |
| og:image:alt | Search Terms Suggestion At the moment, it seems to be very difficult to compose mixins from other mixins. Here's an example on StackOverflow: https://stackoverflow.com/questions/56680049 Here's an ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | trusktr |
| 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