Typescript - 通用类型变量:类型 'T' 不可分配给类型 'T'。存在具有此名称的两种不同类型,但它们不相关

Typescript - Generic Type Variables: Type 'T' is not assignable to type 'T'. Two different types exists with this name exist but they are unrelated

使用打字稿 3.0+。请参阅以下涉及通用类型变量的简单设置:

abstract class BaseClass {
  public abstract merge<T>(model?: T): T;
}

class MyClass extends BaseClass {

  public Value: string;

  public merge<MyClass>(model?: MyClass): MyClass {
    this.Value += model.Value; // <--Property 'Value' does not exist on type 'MyClass'

    return this; // <--Type 'this' is not assignable to type 'MyClass'.
                 //    Type 'MyClass' is not assignable to type 'MyClass'.
                 //    Two different types with this name exist, but they are unrelated.
  }
}

我注意到了 Typescript 编译器描述的错误,但这些错误对我来说没有意义。为什么这是错误的?


已更新

我现在明白了,MyClass 中 merge 方法上方的原始代码定义了一个新的泛型类型变量,其名称与 "MyClass" 相同,这解释了错误。所以我做了如下所示的更改。这仍然会产生一个错误,我在合并方法上面评论道:

abstract class BaseClass {
  public abstract merge<T>(model?: T): T;
}

class MyClass extends BaseClass {

  public Value: string;

/*
Property 'merge' in type 'MyClass' is not assignable to the same property in base type 'BaseClass'.
  Type '(model?: MyClass) => MyClass' is not assignable to type '<T>(model?: T) => T'.
    Types of parameters 'model' and 'model' are incompatible.
      Type 'T' is not assignable to type 'MyClass'.
*/
  public merge(model?: MyClass): MyClass {
    this.Value += model.Value;

    return this;
  }
}

为什么这里的变量类型不能用MyClass?事实上,我似乎无法将它替换为任何其他可以使其工作的类型(例如字符串、数字、另一个 class)。

即使我尝试将 T 定义为扩展 BaseClass 的类型:

abstract class BaseClass {
  public abstract merge<T extends BaseClass>(model?: T): T;
}

这仍然会在 MyClass 中产生相同的错误。请注意,这适用于 TypeScript 2.2.1。我只注意到这不适用于任何 TypeScript 2.4+ 版本。

public merge<MyClass>中的<MyClass>引入了另一种类型(泛型),与class MyClass不同,但同名。对此泛型类型一无所知,因此您会收到错误 Property 'Value' does not exist on type 'MyClass'.

this类型为class MyClassmodel为泛型类型

如果您的基础 class 将方法定义为 merge<T>(model?: T): T,则派生的 class 需要使用相同的定义。你在你的基础 class 中承诺你将处理任何参数(没有任何限制的通用 T),你不能在派生的 class.

中仅将其限制为 MyClass