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
functionmix<Textendsnew(...args: any[])=>any>(S: T){returnclassextendsS{f(){return0;}}};classBase{}constMixed=mix(Base);exportconstm=newMixed();m.f();// Correctly typed in signature helpm.j();// should error here, but doesn't
Well, the reason that you see the apparent type of T is that the any comes from class extends S. S has type T, whose apparent type has a constructor with return type any, which is equivalent to extending any. That’s what allows you to access any property on m.
I though about using InstanceType in the return, that actually makes sense, although the code does throw when expected, it also throws extending S
functionmix<Textendsnew(...args: any[])=>InstanceType<T>>(S: T){returnclassextendsS{// Throws: Base constructor return type 'InstanceType<T>' is not a class or interface type.f(){return0;}}};classBase{}constMixed=mix(Base);exportconstm=newMixed();m.f();// Correctly typed in signature helpm.j();// Throws: Property 'j' does not exist on type 'mix<typeof Base>.(Anonymous class) & Base'.
Maybe I'm using InstanceType poorly, but I think this should be the way to go.
Activity
sandersn commentedon Aug 10, 2018
This is true of Typescript as well:
[-]JavaScript mixin properties not checked[/-][+]mixin properties not checked[/+]sandersn commentedon Aug 13, 2018
Well, the reason that you see the apparent type of T is that the any comes from
class extends S. S has type T, whose apparent type has a constructor with return type any, which is equivalent to extending any. That’s what allows you to access any property on m.michaeljota commentedon Sep 11, 2018
I though about using
InstanceTypein the return, that actually makes sense, although the code does throw when expected, it also throws extendingSMaybe I'm using
InstanceTypepoorly, but I think this should be the way to go.