Here's the code snippet for a function I am working with:
class RestService {
public async get<T>(func: string): Promise<T> {
var toRet = {};
await fetch(EndPoint + func)
.then(response => response.json() as Promise<T>)
.then(data => {
toRet = data;
})
.catch(e => {
});
return toRet as T;
}
}
Everything is functioning properly except for one issue - the response I receive in 'data' always ends up as a generic object.
Let's say I have a model defined like this:
class Model
{
string name;
}
and I make a call to the function like this:
get<Model>("getmodel")
The response consistently appears as a generic object structured like:
{name:"some name"}
As far as my knowledge goes, Typescript does support generics and Promise can handle variable types. My assumption is that perhaps passing a generic into another generic is causing this behavior?