没有过载匹配此调用。使用“next-connect”和“next-iron-session”时出现 TypeScript 错误

No overload matches this call. TypeScript error while using `next-connect` & `next-iron-session`

我有一个项目使用 next-connect & next-iron-session

next-connect 默认使用 <NextApiRequest, NextApiResponse> & 我使用 <NextIronRequest, NextApiResponse> where

export type AppSession = {
  session: Session
}

export type NextIronRequest = NextApiRequest & AppSession

但我无法将 <NextIronRequest, NextApiResponse> 传递给 nc,例如:

return nc<NextIronRequest, NextApiResponse>({
    onError: (err, _, res) => {
      error(err)
      res.status(500).end(err.toString())
    },
  })

这应该可以解决我的问题,但它给我错误:

Expected 0 type arguments, but got 2.ts(2558)

我的 handler() 函数出现过载错误,例如:

export default handler()
  .get('/twitter/generate-auth-link', generateAuthLink)
  .get('/twitter/get-verifier-token', getVerifierToken)

我在每个方法上遇到 2 个错误:

No overload matches this call. Overload 1 of 2, '(...handlers: RequestHandler[]): NextConnect', gave the following error. Argument of type 'string' is not assignable to parameter of type 'RequestHandler'. Overload 2 of 2, '(pattern: string | RegExp, ...handlers: RequestHandler[]): NextConnect', gave the following error. Argument of type '(req: NextIronRequest, res: NextApiResponse) => Promise' is not assignable to parameter of type 'RequestHandler'. Types of parameters 'req' and 'req' are incompatible. Type 'NextApiRequest' is not assignable to type 'NextIronRequest'. Property 'session' is missing in type 'NextApiRequest' but required in type 'AppSession'.ts(2769)

No overload matches this call. Overload 1 of 2, '(...handlers: RequestHandler[]): NextConnect', gave the following error. Argument of type 'string' is not assignable to parameter of type 'RequestHandler'. Overload 2 of 2, '(pattern: string | RegExp, ...handlers: RequestHandler[]): NextConnect', gave the following error. Argument of type '(req: NextIronRequest, res: NextApiResponse) => Promise' is not assignable to parameter of type 'RequestHandler'. Types of parameters 'req' and 'req' are incompatible. Type 'NextApiRequest' is not assignable to type 'NextIronRequest'. Type 'NextApiRequest' is not assignable to type 'AppSession'.ts(2769)

如何解决?

我的完整复制在这里→https://github.com/deadcoder0904/twitter-api-v2-3-legged-login-using-next-connect

事实证明,我使用的是旧版本 next-connect

我已经从 next/examples 克隆了 repo,所以这一定是它在旧版本上的原因。

升级 next-connect 解决了这个问题。