I'm currently working on incorporating the Service Locator pattern into my TypeScript project.
Below is the snippet of my code:
//due to only partial knowledge of TypeScript
private static serviceMap: Map<string, any>;
public static get<T>(): T {
// firstly lazily register all of the necessary services if this is the
// first time calling get.
if(this.serviceMap == undefined){
this.init();
}
let service = this.serviceMap.get(T.name) //issue
if(service == undefined){
throw Error("You must register the service before retrieving it.")
}
return service;
}
The line marked as an issue is causing a problem. I'm attempting to fetch the class type name passed to the method. However, when trying to retrieve T.name, I encounter this error:
TS2693: 'T' only refers to a type, but is being used as a value here.
Is there a way to obtain the name of the class of type T?
As I'm relatively new to TypeScript, I apologize if the solution is straightforward.