考虑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 示例来解释什么是方差、协方差、逆变和双方差?
[持续更新]
有用的链接:
Oleg Valter 的另一个与该主题相关的好答案
Titian-Cernicova-Dragomir对*-rianance 的很好解释
斯蒂芬博耶博客
Scala 文档- 用例子很好的解释
@Titian 的回答 1
@Titian 的回答 2
马克西曼的文章