跳到主要内容
CNode

typescript 里面,如何声明类型为某个类的实例?

问答
JjamieYou发布于 8 年前最后回复 8 年前
240340
class Helper {
  title = ''
  
  static create() {
    return new this
  }
}

class A extends Helper {
  name = ''
}

访问 A.create().name 会报错。

查看回复

回复 (2)

J
jamieYou#28 年前
引用 justjavac官方教程就有:https://www.typescriptlang.org/docs/handbook/generics.html Using Class Types in Generics ...

@justjavac 好像可以了,我之前还以为那个 new(): T 是指函数

J
justjavac#18 年前

官方教程就有:https://www.typescriptlang.org/docs/handbook/generics.html

Using Class Types in Generics

When creating factories in TypeScript using generics, it is necessary to refer to class types by their constructor functions. For example,

function create<T>(c: {new(): T; }): T {
    return new c();
}

参与回复

登录后即可参与回复。登录