Last updated: Feb 26, 2024
Reading time·3 min
To type an async function in TypeScript, set its return type to
Promise<type>
.
Functions marked as async
are guaranteed to return a Promise
even if you
don't explicitly return a value, so the Promise
generic should be used when
specifying the function's return type.
// ✅ Arrow function with Type type GetNumber = (num: number) => Promise<number>; const getNumber: GetNumber = async (num) => { const result = await Promise.resolve(num); return result; }; // -------------------------------------------- // ✅ Arrow function with Interface interface IGetNumber { (num: number): Promise<number>; } const getNumber2: IGetNumber = async (num) => { const result = await Promise.resolve(num); return result; };
The code sample shows how to add type definitions to async arrow functions using a type alias or an interface.
Here are 2 more examples.
// ✅ Arrow function inline const getNumber3 = async (num: number): Promise<number> => { const result = await Promise.resolve(num); return result; }; // --------------------------------------------------- // ✅ Named function inline async function getNumber4(num: number): Promise<number> { const result = await Promise.resolve(num); return result; }
The first two examples use a type and an interface to type the async function.
The syntax is different, but the concept is the same.
Async functions
always return a promise that
resolves with the return value of the async
function.
If the async function throws an error, it returns a Promise, that is rejected with the exception thrown from the async function.
Promise
generic is the type of the value that the async
function returns.Promise<void>
for async functions that don't return a valueIf the async function doesn't return a value, you should
set the return type of the async
function to be Promise<void>
.
async function logNumber(num: number): Promise<void> { await Promise.resolve(); console.log(num); }
Promise<never>
for async functions that throw an errorIf the async function throws an error, you should set its
return type to Promise<never>
.
async function logNumber(num: number): Promise<never> { await Promise.resolve(); throw new Error('Something went wrong'); }
If you forget to use the Promise
generic when specifying the return type of an
async function, you would get a clear error message.
// ⛔️ Error: The return type of an async function or method // must be the global Promise<T> type. // Did you mean to write 'Promise<never>'? async function logNumber(num: number): never { await Promise.resolve(); throw new Error('Something went wrong'); }
async
function is that its return type will always be a promise, regardless if it returns a value, doesn't return a value or throws an error.This is why you should always use the Promise
generic when typing an async
function's return value.
I've also written a detailed guide on how to declare a function with a Promise return type.
If you forget to mark the function as async
, you will get the
'await' expression is only allowed within an async function
error.
You can learn more about the related topics by checking out the following tutorials: