“void”类型上不存在属性“catch”

时间:2021-06-29 13:33:11

标签: typescript express asynchronous try-catch requesthandler

我想做 catchAsync1 但出现错误

<块引用>

'void' 类型不存在属性 'catch'

并想知道 catchAsync3catchAsync2 在打字稿中是否正确

import { NextFunction, Request, RequestHandler, Response } from 'express'

export const catchAsync1 = (handler: RequestHandler) =>
  (...args: [Request, Response, NextFunction]) => handler(...args).catch(args[2])

export const catchAsync2 = (handler: RequestHandler) =>
  (req: Request, res: Response, next: NextFunction) => { try { handler(req, res, next) } catch (err) { next(err) } }

export const catchAsync3 = (handler: RequestHandler) =>
  (...args: [Request, Response, NextFunction]) => { try { handler(...args) } catch (err) { args[2](err) } }

0 个答案:

没有答案