mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
improved singleton types inferring
Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
1 parent
0dbb47a398
commit
a3edb75fea
9 files changed
+17
-16
No files matched your search
@@ -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));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user