-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[2.8.0-dev.20180216] Arguments with mixin type? #21994
Comments
The goal is to emulate the haskell typeclasses. It seems the problem is that |
See https://blog.mariusschulz.com/2017/05/26/typescript-2-2-mixin-classes for an example -- the mixin functions need to be generic or else they just return |
No, thats not related, same thing: type Constructable<T = {}> = new (...args : any[]) => T
class Some {}
function Mixin1<T extends Constructable>(T : Constructable) {
return class OtherModel extends T {
method1() {}
}
}
function Mixin2<T extends Constructable>(T : Constructable) {
return class DiffableModel extends T {
method2() {}
}
}
class Test extends Mixin2(Mixin1(Some)) {
some () {
this.method1()
}
}
let obj = new Test()
const func = (m : InstanceType<ReturnType<typeof Mixin1>>) => false;
func(obj) |
You need to write |
Ah, indeed! Thank you for the help! |
Welcome. Also helped me discover an issue #21995. (Also microsoft/TypeScript-Handbook#15) |
Can you perhaps elaborate a bit on In my initial attempt it was: And correct way is The difference is quite subtle for me, |
If |
Ok, I see. Now one more question if you don't mind. So this code works fine: type Constructable<T = {}> = new (...args : any[]) => T
class Some {}
function Mixin1<T extends Constructable>(Base : T) {
return class OtherModel extends Base {
method1() {}
}
}
function Mixin2<T extends Constructable>(Base : T) {
return class DiffableModel extends Base {
method2() {}
}
}
class Test extends Mixin2(Mixin1(Some)) {
some () {
this.method1()
}
}
let obj = new Test()
const func = (m : InstanceType<ReturnType<typeof Mixin1>>) => false;
func(obj) Now, I want to define an alias, like: export type Mixin<T> = InstanceType<ReturnType<T>> But
Trying export type Mixin<T extends (...args:any[]) => any> = InstanceType<ReturnType<T>> solves the above, but: const func1 = (m : Mixin<typeof Mixin1>) => false;
func1(obj) fails with:
|
I would expect the alias to behave more like a "macros"? |
@samuraijack That looks like a bug: #21997. You can work around it with: // Fixed, see https://github.com/Microsoft/TypeScript/issues/21997
type ReturnType<T extends (...args: any[]) => any> = T extends (...args: any[]) => infer R ? R : never;
type InstanceType<T extends new (...args: any[]) => any> = T extends new (...args: any[]) => infer R ? R : never;
export type Mixin<T extends (...args:any[]) => any> = InstanceType<ReturnType<T>>; |
Thanks! |
The code above does not compile. Any advise how to express this functionality in types?
The intention is to use mixins, and to declare an argument to the function, that is of type, that includes certain mixin. Now that works, but when passing a value, that implements a mixin, a call fails.
The text was updated successfully, but these errors were encountered: