TypeScript: I have a method in the DataProvider class called getTableData:
public static getTableData<T extends DataObject>(type: { new(): T}): Array<T> { ... }
Everything works fine when I use it like this:
let speakers = DataProvider.getTableData(Speaker); // where Speaker is a class
Now I want to invoke this from a generic Class:
export class ViewModelBase<T extends DataObject> {
public getData(): Array<T> {
return <T[]> DataProvider.getTableData(T);
}
}
But now I am facing a Cannot find name 'T' error for the T parameter I pass to getTableData. How should I call getTableData?
Update: Thanks to @Paleo's assistance, I came up with this solution:
export class ViewModelBase<T extends DataObject> {
constructor(private dataObjectClass: { new(): T}){}
public getTableData(): Array<T> {
return <T[]> DataProvider.getTableData<T>(this.dataObjectClass);
}
}
Even though in:
class SpeakerViewModel extends ViewModelBase<Speaker> { ... }
I specified that it is a ViewModel for Speaker
, I still need to instantiate the SpeakerViewModel
like:
let vm = new SpeakerViewModel(Speaker);
It seems like I still don't fully grasp this concept.