Struggling with implementing generic types, I can't seem to make it work properly. It feels like I'm missing something simple. Here's a basic example:
// some module
type TMainResponse<T> = {
data: T;
};
interface Foo {
func<T>(): Promise<TMainResponse<T>>;
}
// local module
type TLocalResponse = {
result: boolean;
};
const obj: Foo = {
async func<TLocalResponse>() {
return {
data: {
result: true
}
};
}
};
(async function () {
await obj.func();
});
The outcome is as follows:
Type 'Promise<{ data: { result: boolean; }; }>' cannot be assigned to type 'Promise<TMainResponse<T>>'.
Type '{ data: { result: boolean; }; }' is not compatible with type 'TMainResponse<T>'.
The types of the 'data' property are incongruent.
Type '{ result: boolean; }' cannot be assigned to type 'T'.
16 async func<TLocalResponse>() {
~~~~
src/test.ts:7:3
7 func<T>(): Promise<TMainResponse<T>>;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The expected type is specified by the 'func' property declared in the 'Foo' interface
I am wondering if there is an error on my end, or if I have simply misunderstood how generics should be used?