#How to chain angle brackets (how to make a specific type from a generic type)?

8 messages · Page 1 of 1 (latest)

fervent thorn
#

I have a getter function. This getter function returns a generic function. I can obtain the type of this generic function as follows:

type GenericFunctionType = ReturnType<typeof getterFunction>;

Above, GenericFunctionType has the type <T>(arg0: whatever, arg1: whatever) => Promise<SomeGenericType<T>>.

Now, I would like to be able to manipulate the type parameter T here. That is, for example I would like GenericFunctionType's type to be <T>(arg0: whatever, arg1: whatever) => Promise<SomeGenericType<{ payload: T }>> instead.

To do so, I've tried type GenericFunctionType<T> = ReturnType<typeof getterFunction><{ payload: T }>; but this is not working.

Is it possible to achieve what I like?

blissful kettle
#
interface SomeGenericType<T> { }
declare function getterFunction<T>(arg0: string, arg1: number): Promise<SomeGenericType<T>>;

type GenericFunctionType<T> = ReturnType<typeof getterFunction<T>>;

like so? @fervent thorn

#

then

const foo: GenericFunctionType<{ payload: number }> = new Promise(() => ({} as SomeGenericType<{ payload: boolean }>));
blissful kettle
#

!resolved

sterile heraldBOT
#

@fervent thorn
Because your issue seemed to be resolved, this post was marked as resolved by @blissful kettle.
If your issue is not resolved, you can reopen this post by running !reopen.
If you have a different question, make a new post in #1057653400046674112.

fervent thorn
#

!reopen

#

I marked this as resolved since at the time I was able to accomplish what I liked in a different way but now I realized that it's not as useful to me as the solution that I seeked initially. I created a playground that explains it better. Would be very happy if you can have a look at it.

sterile heraldBOT
#
utku#6087

Preview:```ts
interface GenericResponse<T> extends Response {
clone(): GenericResponse<T>;
json(): Promise<T>;
}

async function genericFetch<T>(
...args: Parameters<typeof fetch>
) {
return fetch(...args) as Promise<GenericResponse<T>>;
}

// Both getFetcher and fetcher would have arguments but I'm omitting them here to purely focus on the types.
...```