Skip to content

Generics Not Working With This Property #24151

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

Closed
MartinLoeper opened this issue May 16, 2018 · 2 comments
Closed

Generics Not Working With This Property #24151

MartinLoeper opened this issue May 16, 2018 · 2 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@MartinLoeper
Copy link

MartinLoeper commented May 16, 2018

TypeScript Version: 2.9.0-dev.20180515

Search Terms: generics, property, incompatible, polymorphic this, extends constraint

Code

/* taken from mongoose.Document */
interface Document {
    increment(): this;
}

/* our custom model extends the mongoose document */
interface CustomDocument extends Document { }

export class Example<Z extends CustomDocument> {
    constructor() {
        // types of increment not compatible??
        this.test<Z>();
    }

    public test<Z extends Document>() { }
}

Expected behavior:
The type parameter Z should be assignable to the method test as Z is a subtype of CustomDocument which is a subtype of Document.

Actual behavior:
Polymorphic this somehow breaks the functionality.

TS-Error:

error TS2344: Type 'Z' does not satisfy the constraint 'Document'.
  Type 'CustomDocument' is not assignable to type 'Document'.
    Types of property 'increment' are incompatible.
      Type '() => CustomDocument' is not assignable to type '() => Z'.
        Type 'CustomDocument' is not assignable to type 'Z'.

Playground Link: typescriptlang playground

Related Issues: maybe #17316? or #7818?

@DanielRosenwasser DanielRosenwasser added the Bug A bug in TypeScript label May 16, 2018
@DanielRosenwasser DanielRosenwasser added this to the TypeScript 2.9.1 milestone May 16, 2018
@weswigham
Copy link
Member

Fixed by #21210

@weswigham
Copy link
Member

@mhegazy technically this is a dupe of #21117, but this example is much simpler 😉

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label May 19, 2018
@mhegazy mhegazy closed this as completed May 19, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
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

4 participants