I am trying to create an instance of a class based on string input parameter.
for ex:
class Caller{
constructor(private factory: IFactory){}
getInstance(productType: string){
var instance = factory.create(productType);
console.log(instance);
}
}
interface IService{}
class ServiceA implements IService{}
class ServiceB implements IService{}
class IFactory{
create(productType: string): IService;
}
class Factory implements IFactory{
create(productType: string): IService{
// I want to return any child service class ServiceA/ServiceB instance here
// I know I can do conditional instance creation by comparing input string to the type
// but don't want to do that
}
}
I have seen and tried many other similar one's provided on SO, but nothing works for me like: Dynamically loading a typescript class (reflection for typescript)
OR
Creating Instance Of Class Based On ClassName : string
Thanks