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):
typeConstructor<T={}>=new(...args: any[])=>T;classPoint{constructor(publicreadonlyx: number,publicreadonlyy: number,){}}// Tagged is a mixin with a type parameter.constTagged=<T,BextendsConstructor=B>(Base: B)=>classextendsBase{getTag(): T{// Just imagine I have a function that returns objects,// and I want to case them to T.return{}asT}}classTaggedPointextendsTagged(Point){}Now if I want to specify the type argument for Tagged, that's easy enough 1[playground]:
classTaggedPointextendsTagged<number>(Point){}But if I want to pass a derived class's type parameter as that argument, I cannot [playground]:
classUsesTheTag<T>extendsTagged<T>(Point){// ^// error: Base class expressions cannot reference// class type parameters.useTag(){consttag: T=this.getTag()}}Checklist
My suggestion meets these guidelines:
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):
Now if I want to specify the type argument for
Tagged, that's easy enough 1[playground]:But if I want to pass a derived class's type parameter as that argument, I cannot [playground]:
Checklist
My suggestion meets these guidelines:
Footnotes
Note, this requires the default type argument
= Bshown 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? ↩