TypeScript: I have a method in the DataProvider class with a method getTableData:
public static getTableData<T extends DataObject>(type: { new(): T}): Array<T> { ... }
this works perfectly when I code:
let speakers = DataProvider.getTableData(Speaker); // where Speaker is a class
now I want to call this from a generic Class:
export class ViewModelBase<T extends DataObject> {
public getData(): Array<T> {
return <T[]> DataProvider.getTableData(T);
}
}
Now I get a Cannot find name 'T' error for the T parameter I pass to getTableData. How should getTableData be called?
update: With the help of @Paleo I came up this:
export class ViewModelBase<T extends DataObject> {
constructor(private dataObjectClass: { new(): T}){}
public getTableData(): Array<T> {
return <T[]> DataProvider.getTableData<T>(this.dataObjectClass);
}
}
the thing is that although I have already told in:
class SpeakerViewModel extends ViewModelBase<Speaker> { ... }
that I want it to be a ViewModel for Speaker I still have the instantiate the SpeakerViewModel like:
let vm = new SpeakerViewModel(Speaker);
although I have already told it is all about Speaker. I guess I still don't fully understand this.
DataProvider.getTableData(T), the parameterTis defined nowhere.let vm = new SpeakerViewModel(Speaker);" sorry, I am confused. Can you rephrase that? What is the code you want to omit?{ new(...args: any[]): T }. I have a suspicion that it's because my class constructor had parameters.