Im using typescipt 2. Im writing some parser method which receives a model from the server and converts it to an instance I can use.
Code:
export interface Constructable {
copy(other : any) : void;
}
//in my Convert class which converts server responses to model
private static _parseT<T extends Constructable>(obj : any) : T {
let instance = Object.create(T); // the row of the compile-time error
instance.constructor.apply(instance);
instance.copy(obj);
return instance;
}
and lets assume I have the following class
export class Foo implements Constructable {
private test : string = null;
public copy(other:any) : void {
this.test = other.test;
}
}
I have a compile time error
Could not find name T
now, Im sure its not the syntax but I couldn't find how.
To clear things out. this is how the useage looks like:
public static ParseFoo(data: any) : Foo{
return Convert._parseT<Foo>(data.mResult); // data is a server response object
}
NOTE
Although some factory pattern would solve this, I would really like to stay with the Copy method instead of some Generate which creates and returns an instance