You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
exportclassGenericClass<T>{value: T;}exportinterfaceGenericTrait<T>{newGenericValue: T;}exporttypeConstructor<T>=new(...args: any[])=>T;exporttypeConstructable=new(...args: any[])=>object;exportfunctionSimpleMixin<CextendsConstructable>(base: C): C&Constructor<GenericTrait<any>>{returnclassextendsbase{newGenericValue: any;constructor(...args: any[]){super(...args);}};}classChild<T>extendsGenericClass<T>{child: T;constructor(options: {childValue: string}){super();}}constSimpleMixedClass=SimpleMixin(Child);letsimpleMixedInstance=newSimpleMixedClass<string>({childValue: 'three'});// Constructor is maintained//new SimpleMixedClass(3); // Compilation error - good// Generic is maintained//simpleMixedInstance.child = 3; // Compilation error - good// New generic is lostsimpleMixedInstance.newGenericValue=3;// Should be a compilation error
My attempt at a workaround
exportfunctionMixinGeneric<CextendsGenericClass<any>>(base: Constructor<C>): {new<T>(): (GenericClass<T>&GenericTrait<T>&C)}{return<any>classextends(baseasConstructor<GenericClass<any>>){newGenericValue: any;constructor(...args: any[]){super(...args);}};}constMixedClass=MixinGeneric(Child);// Child constructor is lost// let mixedInstance = new MixedClass<string>({ childValue: 'value' }); // Compilation error - badletmixedInstance=newMixedClass<string>();// Child generic type is lostmixedInstance.child={any: 'value'};// Should be a compilation error// New generic type is maintained// mixedInstance.newGenericValue = { anything: 'value' }; // Compilation error - good
Expected behavior:
While I'm not sure if it's the expected behavior currently, the desired behavior is that the generic provided at instantiation time to the SimpleMixedClass will be used as the generic for the GenericTrait as well as the GenericClass.
Actual behavior:
The generic for the GenericTrait is set to any.
The second code example is my attempt at a workaround, but in that case the constructor/generic for any extensions to the targeted base class are lost.
The text was updated successfully, but these errors were encountered:
Any thoughts on this one for TS 2.2 or 2.3? We're in the process of seeing if there are any other issues in switching much of Dojo 2 from dojo/compose to using TS 2.2 classes now that it's easier to support mixins and compositional classes. Thanks!
Similar to here, you need higher kinded types to represent that pattern since you're abstracting both over the constructor type and the type argument for the constructor type.
TypeScript Version: 2.2.0-dev.20170209
Code
My attempt at a workaround
Expected behavior:
While I'm not sure if it's the expected behavior currently, the desired behavior is that the generic provided at instantiation time to the
SimpleMixedClass
will be used as the generic for theGenericTrait
as well as theGenericClass
.Actual behavior:
The generic for the
GenericTrait
is set toany
.The second code example is my attempt at a workaround, but in that case the constructor/generic for any extensions to the targeted base class are lost.
The text was updated successfully, but these errors were encountered: