I am currently working on implementing a basic IOC container with type-checking capabilities.
My goal is to pass the "register" method an abstract class type along with an instance of a derived type.
In the "resolve" function, I aim to provide an abstract class type as input and receive a typed instance that was passed in the "register" method.
How should I define the type for the "instance" parameter in the "register" function? Although I successfully implemented correct type checking for the "resolve" method, the technique using "{prototype: T}" does not seem to work for the "register" method.
const _items = new Map();
export class IoC {
public static register<K>(service: K, instance: any) {
_items.set(service, instance);
}
public static resolve<T>(service: {prototype: T}): T {
return _items.get(service);
}
}
// example of usage
IoC.register(AppServer, new TestAppServer());
IoC.resolve(AppServer)