typescript 里面,如何声明类型为某个类的实例?
class Helper {
title = ''
static create() {
return new this
}
}
class A extends Helper {
name = ''
}
访问 A.create().name 会报错。
2 回复
官方教程就有: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();
}
@justjavac 好像可以了,我之前还以为那个 new(): T 是指函数