export class Singleton {
public static Inst<T extends Singleton>(this: new (args?: any) => T, args = undefined): T {
const cls = this as any;
if (cls.instance) {
return cls.instance
}
return cls.instance = new cls(args)
}
}
// For Example:
class Sample extends Singleton {
doSomething(): void {
}
}
Sample.Inst().doSomething();
此处 Sample.Inst() 获取的是 Sample 实例, 不是 Singleton,类型提示友好。