Title: Allow type parameters in base class expressions · Issue #26542 · microsoft/TypeScript · GitHub
Open Graph Title: Allow type parameters in base class expressions · Issue #26542 · microsoft/TypeScript
X Title: Allow type parameters in base class expressions · Issue #26542 · microsoft/TypeScript
Description: Search Terms typescript type parameters in base class expressions Suggestion Type parameters in base class expressions. Use Cases and Examples Let's start with a simple mixin [playground] (borrowed from proposal #13743): type Constructor...
Open Graph Description: Search Terms typescript type parameters in base class expressions Suggestion Type parameters in base class expressions. Use Cases and Examples Let's start with a simple mixin [playground] (borrowed...
X Description: Search Terms typescript type parameters in base class expressions Suggestion Type parameters in base class expressions. Use Cases and Examples Let's start with a simple mixin [playground] (borr...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/26542
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Allow type parameters in base class expressions","articleBody":"## Search Terms\r\n\r\ntypescript type parameters in base class expressions\r\n\r\n## Suggestion\r\n\r\nType parameters in base class expressions.\r\n\r\n## Use Cases and Examples\r\n\r\nLet's start with a simple mixin [[playground]](https://www.typescriptlang.org/play/#src=type%20Constructor%3CT%20%3D%20%7B%7D%3E%20%3D%20new%20(...args%3A%20any%5B%5D)%20%3D%3E%20T%3B%0D%0A%0D%0Aclass%20Point%20%7B%0D%0A%20%20constructor(%0D%0A%20%20%20%20public%20readonly%20x%3A%20number%2C%0D%0A%20%20%20%20public%20readonly%20y%3A%20number%2C%0D%0A%20%20)%20%7B%7D%0D%0A%7D%0D%0A%0D%0Aconst%20Tagged%20%3D%20%3CT%2C%20B%20extends%20Constructor%20%3D%20B%3E(Base%3A%20B)%20%3D%3E%0D%0A%20%20class%20extends%20Base%20%7B%0D%0A%20%20%20%20getTag()%3A%20T%20%7B%0D%0A%20%20%20%20%20%20%2F%2F%20Just%20imagine%20I%20have%20a%20function%20that%20returns%20objects%2C%0D%0A%20%20%20%20%20%20%2F%2F%20and%20I%20want%20to%20case%20them%20to%20T.%0D%0A%20%20%20%20%20%20return%20%7B%7D%20as%20T%0D%0A%20%20%20%20%7D%0D%0A%20%20%7D%0D%0A%0D%0Aclass%20TaggedPoint%20extends%20Tagged(Point)%20%7B%7D) (borrowed from proposal #13743):\r\n\r\n```typescript\r\ntype Constructor\u003cT = {}> = new (...args: any[]) => T;\r\n\r\nclass Point {\r\n constructor(\r\n public readonly x: number,\r\n public readonly y: number,\r\n ) {}\r\n}\r\n\r\n// Tagged is a mixin with a type parameter.\r\nconst Tagged = \u003cT, B extends Constructor = B>(Base: B) =>\r\n class extends Base {\r\n getTag(): T {\r\n // Just imagine I have a function that returns objects,\r\n // and I want to case them to T.\r\n return {} as T\r\n }\r\n }\r\n\r\nclass TaggedPoint extends Tagged(Point) {}\r\n```\r\n\r\nNow [if I want to specify the type argument for `Tagged`](https://stackoverflow.com/questions/48492002/passing-generic-types-through-to-mixins-in-typescript), that's easy enough [^1] [[playground]](https://www.typescriptlang.org/play/#src=type%20Constructor%3CT%20%3D%20%7B%7D%3E%20%3D%20new%20(...args%3A%20any%5B%5D)%20%3D%3E%20T%3B%0D%0A%0D%0Aclass%20Point%20%7B%0D%0A%20%20constructor(%0D%0A%20%20%20%20public%20readonly%20x%3A%20number%2C%0D%0A%20%20%20%20public%20readonly%20y%3A%20number%2C%0D%0A%20%20)%20%7B%7D%0D%0A%7D%0D%0A%0D%0Aconst%20Tagged%20%3D%20%3CT%2C%20B%20extends%20Constructor%20%3D%20B%3E(Base%3A%20B)%20%3D%3E%0D%0A%20%20class%20extends%20Base%20%7B%0D%0A%20%20%20%20getTag()%3A%20T%20%7B%0D%0A%20%20%20%20%20%20%2F%2F%20Just%20imagine%20I%20have%20a%20function%20that%20returns%20objects%2C%0D%0A%20%20%20%20%20%20%2F%2F%20and%20I%20want%20to%20case%20them%20to%20T.%0D%0A%20%20%20%20%20%20return%20%7B%7D%20as%20T%0D%0A%20%20%20%20%7D%0D%0A%20%20%7D%0D%0A%0D%0Aclass%20TaggedPoint%20extends%20Tagged%3Cnumber%3E(Point)%20%7B%7D):\r\n\r\n```typescript\r\nclass TaggedPoint extends Tagged\u003cnumber>(Point) {}\r\n```\r\n\r\nBut if I want to pass a derived class's type parameter as that argument, I cannot [[playground]](https://www.typescriptlang.org/play/#src=type%20Constructor%3CT%20%3D%20%7B%7D%3E%20%3D%20new%20(...args%3A%20any%5B%5D)%20%3D%3E%20T%3B%0D%0A%0D%0Aclass%20Point%20%7B%0D%0A%20%20constructor(%0D%0A%20%20%20%20public%20readonly%20x%3A%20number%2C%0D%0A%20%20%20%20public%20readonly%20y%3A%20number%2C%0D%0A%20%20)%20%7B%7D%0D%0A%7D%0D%0A%0D%0Aconst%20Tagged%20%3D%20%3CT%2C%20B%20extends%20Constructor%20%3D%20B%3E(Base%3A%20B)%20%3D%3E%0D%0A%20%20class%20extends%20Base%20%7B%0D%0A%20%20%20%20getTag()%3A%20T%20%7B%0D%0A%20%20%20%20%20%20%2F%2F%20Just%20imagine%20I%20have%20a%20function%20that%20returns%20objects%2C%0D%0A%20%20%20%20%20%20%2F%2F%20and%20I%20want%20to%20case%20them%20to%20T.%0D%0A%20%20%20%20%20%20return%20%7B%7D%20as%20T%0D%0A%20%20%20%20%7D%0D%0A%20%20%7D%0D%0A%0D%0Aclass%20UsesTheTag%3CT%3E%20extends%20Tagged%3CT%3E(Point)%20%7B%0D%0A%2F%2F%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5E%0D%0A%2F%2F%20error%3A%20Base%20class%20expressions%20cannot%20reference%0D%0A%2F%2F%20class%20type%20parameters.%0D%0A%20%20useTag()%3A%20T%20%7B%0D%0A%20%20%20%20const%20tag%3A%20T%20%3D%20this.getTag()%0D%0A%20%20%20%20return%20tag%0D%0A%20%20%7D%0D%0A%7D):\r\n\r\n```typescript\r\nclass UsesTheTag\u003cT> extends Tagged\u003cT>(Point) {\r\n// ^\r\n// error: Base class expressions cannot reference\r\n// class type parameters.\r\n useTag() {\r\n const tag: T = this.getTag()\r\n }\r\n}\r\n```\r\n\r\n[^1]: Note, this requires the default type argument `= B` shown above, but I have never seen it used in documentation, Stack Overflow, or blogs on the issue of TypeScript mixins. Is that because TypeScript is not actually deducing the type parameter from the function argument?\r\n\r\n## Checklist\r\n\r\nMy suggestion meets these guidelines:\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. new expression-level syntax)\r\n\r\n","author":{"url":"https://github.com/thejohnfreeman","@type":"Person","name":"thejohnfreeman"},"datePublished":"2018-08-19T22:35:18Z","url":"https://github.com/microsoft/TypeScript/issues/26542"}
| 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:bc9258d9-de1c-f3ec-6632-823f0d6ba1f5 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C7A4:10D9F8:6A8864:8F1ED6:6B00D48C |
| html-safe-nonce | ef5ab2605673bd612a10d9c501296ddf4cf86c993d513eab8e65b439591cc131 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDN0E0OjEwRDlGODo2QTg4NjQ6OEYxRUQ2OjZCMDBENDhDIiwidmlzaXRvcl9pZCI6IjI2ODQ5Mzg4OTYzOTA4MDQ2MjAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | f2432c6e7dfd24ad70ca50d3e388731e93db17eb51a001a141af3f07f6f55532 |
| hovercard-subject-tag | issue:351940497 |
| 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/26542/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a94f0799be26a3f8f334add42bd7db4a9619a04ab1622a87aa6522067afa838d/microsoft/TypeScript/issues/26542 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a94f0799be26a3f8f334add42bd7db4a9619a04ab1622a87aa6522067afa838d/microsoft/TypeScript/issues/26542 |
| og:image:alt | Search Terms typescript type parameters in base class expressions Suggestion Type parameters in base class expressions. Use Cases and Examples Let's start with a simple mixin [playground] (borrowed... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | thejohnfreeman |
| hostname | github.com |
| expected-hostname | github.com |
| None | a481f62a1e04c9b9bdcb5628230682c66d59006f5b13b080f79cba84a176905b |
| 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 | 84c8c5cc25e2fb7f2308428458538436baecc6d2 |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width