如何实现泛型抽象类?

时间:2019-04-16 12:28:19

标签: typescript abstract-class typescript2.0

我有抽象类:

abstract class AScore<T> {
  constructor(
    protected data: T) {}
}

我实现此类:

class GetActivitiesPupil implements AScore<number> {}

编译器说这是错误的类实现

1 个答案:

答案 0 :(得分:3)

您要扩展抽象类以创建具体实例,即

abstract class AScore<T> {
  constructor(protected data: T) {}
}

class GetActivitiesPupil extends AScore<number> {
  data: number;

  constructor(data: number) {
   super(data)}
  }
}

const test = new GetActivitiesPupil(123);
console.log(test.data) // Outputs 123

您可以看到此代码没有错误here