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

考虑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;
    }
}

删除私有财产name将使错误消失,但不是我现实世界问题的解决方案。奇怪的是,没有泛型的同一个例子工作得很好:

interface ICat
{
    likes( other: ICat ): boolean;
}

class Cat implements ICat
{
    private name = "Simba";
    public likes( other: Cat )  // no error using Cat here (instead of ICat)
    {
        return true;
    }
}

我在这里缺少什么?

以上是为什么实现接口的类中的泛型成员函数不能采用类(而不是接口)类型的参数?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>