335200
class Singleton {
private constructor(...args: any[]) {}
static instance = new Singleton()
}
这种,不能被继承,可以将 constructor 改为 protected:
class Singleton {
protected constructor(...args: any[]) {}
private static instance: any // 问题在这里
static getInstance() {
return this.instance ||= new this()
}
}
但,getInstance 的返回值是 any 类型
最后,我尝试了 Generic,但 instance 是个静态属性
感谢同仁指点