improved singleton types inferring

Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman committed 2020-07-17 20:00:47 +03:00
1 parent 0dbb47a398
commit a3edb75fea
9 files changed
+17 -16

No files matched your search

+4 -2
View File
@@ -6,11 +6,13 @@
* const usersStore: UsersStore = UsersStore.getInstance();
*/
type Constructor<T = {}> = new (...args: any[]) => T;
class Singleton {
private static instances = new WeakMap<object, Singleton>();
// todo: figure out how to infer child class + arguments types
static getInstance<T extends Singleton>(...args: any[]): T {
// todo: improve types inferring
static getInstance<T>(...args: ConstructorParameters<Constructor<T>>): T {
if (!Singleton.instances.has(this)) {
Singleton.instances.set(this, Reflect.construct(this, args));
}