相关疑难解决方法(0)

为什么实现接口的类中的泛型成员函数不能采用类(而不是接口)类型的参数?

考虑IDog与方法的接口likes<T extends IDog>( other: T )。该方法采用一个类型扩展接口的参数。为什么不允许在派生类中Dog使用类作为参数类型而不是接口来实现该方法?

interface IDog
{
    likes<T extends IDog>( other: T ): boolean;
}

class Dog implements IDog
{
    private name = "Buddy";
    public likes<T extends Dog>( other: T )
        // ^^^^^
        // error: Property 'likes' in type 'Dog' is not 
        // assignable to the same property in base type 'IDog' [...]
        // Property 'name' is missing in type 'IDog' but required in type 'Dog'
    {
        return true;
    }
}
Run Code Online (Sandbox Code Playgroud)

删除私有财产 …

typescript typescript-types

5
推荐指数
1
解决办法
61
查看次数

TypeScript 中的方差、协方差、逆变和双方差的区别

您能否使用小而简单的 TypeScript 示例来解释什么是方差、协方差、逆变和双方差?

[持续更新]

有用的链接:

  1. Oleg Valter 的另一个与该主题相关的好答案

  2. Titian-Cernicova-Dragomir对*-rianance 的很好解释

  3. 斯蒂芬博耶博客

  4. Scala 文档- 用例子很好的解释

  5. @Titian 的回答 1

  6. @Titian 的回答 2

  7. Vlad Riscutia 的博客

  8. 马克西曼的文章

covariance contravariance variance typescript invariance

4
推荐指数
1
解决办法
250
查看次数