Categories
TypeScript Answers

How to define type of async function with TypeScript?

Spread the love

Sometimes, we want to define type of async function with TypeScript.

In this article, we’ll look at how to define type of async function with TypeScript.

How to define type of async function with TypeScript?

To define type of async function with TypeScript, we can set the return type of the function to Promise.

For instance, we write

type SearchFn = (subString: string) => Promise<boolean>;

to create the SearchFn type which is set to a function type with the return type of Promise<boolean>.

This means any function with type SearchFn must return a promise that resolves to a boolean.

Conclusipon

To define type of async function with TypeScript, we can set the return type of the function to Promise.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *