Skip to content
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

Closed
shlomiassaf opened this issue Apr 4, 2016 · 8 comments
Closed

Polymorphic 'this' with Generics #7818

shlomiassaf opened this issue Apr 4, 2016 · 8 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@shlomiassaf
Copy link

TypeScript Version:
typescript@1.8.9
typescript@1.9.0-dev.20160404

Code

// 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
// 2 type parameters, T (with constraint) and Z also with constraint related to T.
export class ParentFactory<T extends ParentData, Z extends Parent<T>> {}
export class ChildFactory<T extends ChildData, Z extends Child<T>> extends ParentFactory<T, Z> {}

Expected behavior:
No errors...

Actual behavior:

error TS2344: Type 'Z' does not satisfy the constraint 'Parent<T>'.
  Type 'Child<T>' is not assignable to type 'Parent<T>'.
    Types of property 'toBeMethod' are incompatible.
      Type '(value: string) => Child<T>' is not assignable to type '(value: string) => Z'.
        Type 'Child<T>' is not assignable to type 'Z'.

Type 'Child' is not assignable to type 'Z'.

How Child is not assignable to type 'Z' if:
export class ChildFactory<T extends ChildData, Z extends Child> extends ParentFactory<T, Z> {}

@RyanCavanaugh
Copy link
Member

Is this distinct from #6223?

@shlomiassaf
Copy link
Author

An error isn't specified there, so it's hard to tell.
The compiled code works when I run it, I just get that error...

It's seems legit if I look at the constraints...

@shlomiassaf
Copy link
Author

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.

@mhegazy
Copy link
Contributor

mhegazy commented Apr 5, 2016

i believe this is a bug. we are not instantiating the constraint with correct this argument before doing the comparison.

@mhegazy mhegazy added the Bug A bug in TypeScript label Apr 5, 2016
@mhegazy mhegazy added this to the TypeScript 2.0 milestone Apr 5, 2016
@shlomiassaf
Copy link
Author

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:

error TS2344: Type 'T' does not satisfy the constraint 'Parent<ParentData>'.
  Type 'Child<ChildData>' is not assignable to type 'Parent<ParentData>'.
    Types of property 'toBeMethod' are incompatible.
      Type '(value: string) => Child<ChildData>' is not assignable to type '(value: string) => T'.
        Type 'Child<ChildData>' is not assignable to type 'T'.

@dsherret
Copy link
Contributor

dsherret commented May 2, 2016

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
}

@mhegazy
Copy link
Contributor

mhegazy commented May 2, 2016

should be fixed by #7290

@mhegazy mhegazy closed this as completed May 2, 2016
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label May 2, 2016
@mhegazy mhegazy assigned DanielRosenwasser and unassigned sandersn May 2, 2016
@dsherret
Copy link
Contributor

dsherret commented May 2, 2016

This fixed the issue for me. Thank you!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

6 participants