-
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
Polymorphic 'this' with Generics #7818
Comments
Is this distinct from #6223? |
An error isn't specified there, so it's hard to tell. It's seems legit if I look at the constraints... |
In #6223 it's referring to not suppling a generic argument vs suppling one... so I'm not sure but it might be related to the same underlying bug/not a feature. |
i believe this is a bug. we are not instantiating the constraint with correct this argument before doing the comparison. |
Here's a simpler sample that causes the error, with only 1 type parameter // 2 Simple interfaces, child extends parent.
interface ParentData {
parentData: string;
}
interface ChildData extends ParentData {
childData: string;
}
// 2 Simple classes with Generics.
// Each class has a "placeholder" method.
// Type parameter must satisfy the XData interface above.
class Parent<T extends ParentData> {
toBeMethod: (value: string) => this;
}
// Here we have 2 "extends"
// 1) Type parameter T extends ChildData interface (which extends ParentData)
// 2) Child<T> extends Parent<T>, Parent will have a type parameter T that is ChildData (at least)
class Child<T extends ChildData> extends Parent<T> {
yetAnotherToBeMethod: (value: string) => this;
}
// NOTE: The "placeholder" methods have a return signature of 'this', this + generics cause's the issue.
// These are factories for creating instances of Parent,Child
export class ParentFactory<T extends Parent<ParentData>> {}
export class ChildFactory<T extends Child<ChildData>> extends ParentFactory<T> {} Error:
|
Here's another example: abstract class Class1 {
myMethod() {
return this;
}
}
class Class2 extends Class1 {
}
abstract class Test1<T extends Class1> {
}
class Test2<T extends Class2> extends Test1<T> { // error: type Class2 is not assignable to T
} |
should be fixed by #7290 |
This fixed the issue for me. Thank you! |
TypeScript Version:
typescript@1.8.9
typescript@1.9.0-dev.20160404
Code
Expected behavior:
No errors...
Actual behavior:
How Child is not assignable to type 'Z' if:
export class ChildFactory<T extends ChildData, Z extends Child> extends ParentFactory<T, Z> {}
The text was updated successfully, but these errors were encountered: