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
.