I have a service which I want to use to return a object of a specified type build from a JSON.
I have a class MyClass which implements a static class which define a FromJSON static method.
export interface InterfaceMyClass {
static FromJSON(json: any): any;
}
export class MyClass implements InterfaceMyClass {
constructor(){}
FromJSON(json: any): MyClass {
let instance = MyClass.create(MyClass.prototype);
Object.assign(instance, json);
// Some other code specific to MyClass
return instance;
}
}
I don't know how to call the static method of the generic class that I've passed in parameter in my service. My service looks like this:
export class MyService<T extends InterfaceMyClass> {
getObject() {
let json = getExternalJson(...);
return T.FromJSON(json); // <-- How to call static method FromJSON from T class ?
}
}
I would like to use the service this way:
let service = new MyService<MyClass>();
let myObject = service.getObject(); // <-- Should by an MyClass instance (created by MyClass.FromJSON)
Question:
How can I call this T.FromJSON method?
Bonus question: What is the good way to implements static method? I dont think that my FromJSON method from MyClass is static. If I add static word before FromJSON, it tells me this:
[ts] Class 'MyClass' incorrectly implements interface 'InterfaceMyClass'.
Property 'FromJSON' is missing in type 'MyClass'.