-
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-rc] Mixins don't recognize static method from parent class #22788
Comments
A class has two parts, a constructor function that also holds the static methods and properties, and an instance type that holds the instance methods and properties. the constructor function returns values of the instance type. your function declaration takes a |
Hm.. Can you please provide an example how to do that? |
The |
Additional information: It does seem to work, for 2nd level mixin. This is strange, shouldn't the behavior be consistent? export type Constructable<T = {}> = new (...args : any[]) => T
class Base {
constructor(...args : any[]) {
}
static someStaticMethod() : number {
return 1
}
someInstanceMethod() : number {
return 1
}
}
const Mixin1 = <T extends Constructable<Base>>(base : T) => class Some1 extends base {
static someStaticMethod () : number {
return 1
// return super.someStaticMethod() + 1 // !!! <----- compiler error here
}
someInstanceMethod () {
return super.someInstanceMethod() + 1
}
}
const Mixin2 = <T extends Constructable<Base>>(base : T) => class Some1 extends Mixin1<Constructable<Base>>(base) {
static someStaticMethod () {
return super.someStaticMethod() + 1 // !!! <----- this compiles fine
}
someInstanceMethod () {
return super.someInstanceMethod() + 1
}
} |
@samuraijack In In |
@cspotcode but why you say that
as @mhegazy mentioned
This means, that If typechecker is clever enough to figure out the presence of static method from the mixin, its should be clever enough to figure it out from the base class as well I think. |
Correct, and There's a big difference between |
@cspotcode Ok, I think I understand it vaguely. Any idea how to express this static methods inheritance in types? |
@cspotcode Oh, looks interesting, thanks! It seems to work, the mixin declaration becomes a bit cluttered though. |
@cspotcode Thanks for the inspiration, in fact, just changing the mixin's generic parameter to export type Constructable<T = {}> = new (...args : any[]) => T
class Base {
constructor(...args : any[]) {
}
static someStaticMethod() : number {
return 1
}
someInstanceMethod() : number {
return 1
}
}
const Mixin1 = <T extends typeof Base>(base : T) => class Some1 extends base {
static someStaticMethod () : number {
return 1
// return super.someStaticMethod() + 1
}
someInstanceMethod () {
return super.someInstanceMethod() + 1
}
}
const Mixin2 = <T extends typeof Base>(base : T) => class Some1 extends Mixin1<typeof Base>(base) {
static someStaticMethod () {
return super.someStaticMethod() + 1
}
someInstanceMethod () {
return super.someInstanceMethod() + 1
}
} In your example the const MyMixin = <C extends typeof Foo>(Base: C) => class MyMixin extends (Base as typeof Foo) {
static mixinStatic() {
super.staticMethod()
}
} I guess, this should be a new "default" mixins declaration for the case, when one assumes some specific base class. Deserves a blog post :) |
@samuraijack that's why my workaround uses two generic declarations and a nested function within the mixin function. I believe it should workaround that issue, right? But yeah it's more syntax. |
Could not check, because it would have been too cumbersome to replace all mixins declarations (the |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
When declaring a mixin, one can provide a "default" parent class to inherit from, using the type argument for the "Constructable" type. When this is done, TypeScript correctly finds the normal methods from the parent class, however it can not find the static methods:
The text was updated successfully, but these errors were encountered: