-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
InstanceType<(typeof this)["somefield"]> type issue #51013
Comments
There are too many things wrong with this code but I'll just leave a good solution class S1 {}
class S2 extends S1 {}
class A<X extends new (...args: any[]) => any> {
constructor(public cls: X) {}
get x(): X {
return this.cls
}
f(): X {
const o = new this.x()
return o;
}
}
class B<X extends new (...args: any[]) => any> extends A<X> {
f1(){
return this.f();
}
}
const a = new A(S1);
const s1test = a.f() // S1 type
// ^?
const b = new B(S2);
const s2test = b.f1() // S2 type
// ^? |
thanks, but based on current architecture (including dependency injector) i can't accept your solution. |
Personally I'll suggest avoid using conditional types as return type of a function, in your case you could use |
@imsamurai Here is the one that correctly returns the type class S1 {}
class S2 extends S1 {}
class A<X extends new (...args: any[]) => any> {
constructor(public cls: X) {}
get x(): X {
return this.cls
}
f(): InstanceType<X> {
return new this.x();
}
}
class B<X extends new (...args: any[]) => any> extends A<X> {
f1(){
return this.f();
}
}
const a = new A(S1);
const s1test = a.f() // S1 type
// ^?
const b = new B(S2);
const s2test = b.f1() // S2 type
// ^? |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Hi, for inheritance purposes i need to type methods depending on class returned by getter, but i get 'is not assignable' error.
Is this a bug?
https://www.typescriptlang.org/play?#code/MYGwhgzhAEDKCM0DeBfAUKSNYCZoFMAPAF3wDsATbRVNOzKaAQWmTVegHN9jpCAKAJRJ2HAE48ArmLJx4o9KIBmQgFzQAkmQjEwZYPgAqATwAO+ADzEAFgEsIAbQBEhJwF0AfG3FSZ0MvgA7tA29gB0hNCQmtq6+kZmlqGOLu5eAPTp-gD2BGJi2WIKdKxKAKL5hWoxOnoGJuZWdimunt6sEsTSsgHByRHQmVzZvPiVRazoihjgjABCBCTkVMzt3LwCgu0dvrK4xcrwQiIc0J3dIc1hKoIA3MXTwNmxUdAAvP5BzPdPLxDwpB07yi1yEgyyCBCiToGGeQIARsDetA5j84bwIDhAbwPvDrkctkNcFDzEA
The text was updated successfully, but these errors were encountered: