tg_stat_bot/src/utils/ioc.ts

22 lines
454 B
TypeScript

class IOC {
private readonly items = new Map<Symbol, any>();
public set<T>(key: Symbol, obj: T): void {
this.items.set(key, obj);
}
public get<T>(key: Symbol): T {
return this.items.get(key) as T;
}
public create<A extends any[], T extends { new(...args: A): T; }>(
key: Symbol,
...args: A
): T {
const cls = this.get(key) as { new(...args: A): T; };
return new cls(...args);
}
}
export const ioc = new IOC();