类型是什么类型?

时间:2019-03-19 16:31:50

标签: typescript

类型是什么类型?

这听起来似乎很混乱,所以一段代码可能不仅仅包含单词。

// let's say there is some interface
interface A {
}

// and there is a class that implements it.
// there are a lot of implementations of A.
// but the AImpl is the current/best one.
class AImpl implements A {
}

// and then there is some kind of repository.
// which keeps track of these types, and knows which one is the best.
class Repository {

  // so, this repository returns the most current type.
  static getTypeForA(): Function {
    return AImpl;
  }
}

有点奇怪,对吧?它返回类型为Function的实例,而实际上它返回一个类。它可以正常工作,因为类毕竟是一个函数。

但是可能还有更好的东西,对吧?我应该使用哪种类型?

1 个答案:

答案 0 :(得分:0)

您可能期望<cbnp-message:cbnp-message xmlns:cbnp-message="some_value" xmlns="some_value2"/> 返回接口getTypeForA的实现。因此返回类型应该是接口A

A