Description
I don't know if it is intended. But it seems very hard for users to understand.
Note in the #15104, it seems type parameter in generic type is checked co-variantly only when the type parameter is used in the parameter position of a callback (that is, a contravariant position in another contravariant position makes a cavariant position).
However, in the following example, Done
is a plain function, not callback. So A
is contravariantly checked. This might confuse old TS users.
TypeScript Version: 2.4.1
Code
class Animal {
private animalTag: any
}
class Dog extends Animal {
private dogTag: any
}
function trainDog(d: Dog) { }
type Done = <A extends Animal>(result: A) => void
function cloneAnimal(done: Done): void {}
function cloneAnimal2(done: (result: Animal) => void): void {}
cloneAnimal(trainDog); // error
cloneAnimal2(trainDog); // compiles
Expected behavior:
All compiles.
Actual behavior:
cloneAnimal
fails to compile. Argument of type '(d: Dog) => void' is not assignable to parameter of type 'Done'. Types of parameters 'd' and 'result' are incompatible. Type 'A' is not assignable to type 'Dog'. Type 'Animal' is not assignable to type 'Dog'. Property 'dogTag' is missing in type 'Animal'.