Closed
Description
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