strictFunctionTypes restricts generic type

前端 未结 1 1694
心在旅途
心在旅途 2020-12-21 15:14

The problem seems to be specific to how strictFunctionTypes affects generic class type.

Here is a class that closely reproduces what happens and cannot be simplified

相关标签:
1条回答
  • 2020-12-21 15:26

    This is at it's core an issue of variance. So first a variance primer:

    About variance

    Given a generic type Foo<T>, and two related types Animal and Dog extends Animal. There are four possible relationships between Foo<Animal> and Foo<Dog>:

    1. Covariance - The arrow of inheritance points in the same direction for Foo<Animal> and Foo<Dog> as it does for Animal and Dog, so Foo<Dog> is a sub type of Foo<Animal>, which also means Foo<Dog> is assignable to Foo<Animal>
    type CoVariant<T> = () => T
    declare var coAnimal: CoVariant<Animal>
    declare var coDog: CoVariant<Dog>
    coDog = coAnimal; //                                                                     
    0 讨论(0)
提交回复
热议问题