These TS typing improvements address the following:
Consider that usually a Promise is passed to the expressAsyncHandler function. Without this fix newer ESLint versions will produce the following error:
Promise returned in function argument where a void return was expected @typescript-eslint/no-misused-promises
Pass Express’ generics for P, ResBody, ReqBody, and ReqQuery to the wrapped handler function. This provides an additional level of type-safety if being used:
const handler = async (
req: express.Request<{ foo: string; bar: number }>,
res: express.Response
): Promise<void> => {
// req.params.foo is string
// req.params.bar is number
// ...
};
router.get<{ foo: string; bar: number }>('/:foo/:bar', asyncHandler(handler));
// gives error:
router.get<{ foo: string; bar: string }>('/:foo/:bar', asyncHandler(handler));
These TS typing improvements address the following:
Consider that usually a
Promise
is passed to theexpressAsyncHandler
function. Without this fix newer ESLint versions will produce the following error:Pass Express’ generics for
P
,ResBody
,ReqBody
, andReqQuery
to the wrapped handler function. This provides an additional level of type-safety if being used: