How to Add a Type to an Async Function in TypeScript

Published Feb 6, 2022  ∙  Updated May 2, 2022

How can we define a type for an async function in TypeScript?

Suppose we have an async function handler().

const handler = async (str: string) => {}

How can we properly add a type to this function?

What does async imply?

An async function implies that we will use await in that function.

What this further implies is that the function will return a Promise.

I would encourage you to read about the relationship between Promises and async/await.

Add type to async function

Finally, we can define the return type of an async function to be a Promise. If we do this, we don’t need to declare the function to be async.

Let’s remove the async keyword, and return a Promise<string>.

const handler = (str: string): Promise<string> => {}