Skip to content

Generics Not Working With This Property #24151

Closed
@MartinLoeper

Description

@MartinLoeper

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?

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions