The TypeScript error "Promise returned in function argument where a void
return was expected" occurs because an async
function always returns a
Promise, but the context where it is used expects a function that returns
void
(i.e., no value). This is common in event handlers or callbacks that do
not handle promises.
Explanation
- An
async
function implicitly returns a Promise, even if you don't explicitly return one. - Some APIs or event handlers expect a function that returns
void
because they do not handle or await promises. - Passing an
async
function in such a context causes a type mismatch because TypeScript expectsvoid
but receivesPromise<void>
.
Example
ts
process.on("uncaughtException", async (error: Error) => {
await this.errorHandler(error);
});
This causes the error because process.on
expects a callback returning
void
, but the async callback returns a Promise<void>
How to fix or work around
-
Use a non-async function and call the async function without awaiting it, e.g.,
ts
process.on("uncaughtException", (error: Error) => { this.errorHandler(error); // call but don't await });
-
Alternatively, explicitly ignore the returned promise using
void
operator:ts
process.on("uncaughtException", (error: Error) => { void this.errorHandler(error); });
The void
operator evaluates the expression and returns undefined
,
effectively discarding the promise, which satisfies the void
return type
expectation
Why TypeScript enforces this
TypeScript warns about this to prevent unhandled promise rejections and to make sure you are aware that the promise is not awaited or handled. This helps avoid subtle bugs where asynchronous errors might be swallowed silently
. In summary, the error means you passed an async function (returning a
Promise) where a synchronous void-returning function was expected. The
solution is to either remove async
and not await inside, or explicitly
ignore the promise with void
to satisfy the expected return type. This is a
TypeScript type safety feature to avoid unintentional ignored promises.