Skip to content

Latest commit

 

History

History
47 lines (36 loc) · 1.69 KB

get-the-return-type-of-an-async-function.md

File metadata and controls

47 lines (36 loc) · 1.69 KB

Get The Return Type Of An Async Function

When working with TypeScript-first libraries like Prisma, you'll be working with functions that have useful, but complex type signatures. The TypeScript built-in Utility Types make a big difference when working with these.

For instance, a function that makes a couple Prisma calls with join'ed data will have a large return type that you can't easily recreate with imported types from the Prisma client.

Given a function like this, we can start out extracting its return type with the ReturnType utility type, passing it the typeof the function as the generic.

type FuncReturnType = ReturnType<typeof getPostsForUser>

A function like this is going to be async, so its return type will be wrapped in a promise. We can "unwrap" the promise with Awaited.

type FuncReturnType = ReturnType<typeof getPostsForUser>
type ResolvedFuncReturnType = Awaited<FuncReturnType>

We are often querying for lists of things, so the result will be an array of the type we are interested in. We can extract the type of an array with [number].

type FuncReturnType = ReturnType<typeof getPostsForUser>
type ResolvedFuncReturnType = Awaited<FuncReturnType>
type PostType = ResolvedFuncReturnType[number]

Putting it all together into a single line looks like this:

type Post = Awaited<ReturnType<typeof getPostsForUser>>[number]

source