NextJS 服务器文件中的 Typescript 错误

Typescript error in server file for NextJS

我正在使用以下软件包版本:

server.js

import next from 'next';
const app = next({ dev: isDevelopment });

tsconfig.json

{
  "compilerOptions": {
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "alwaysStrict": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "jsx": "preserve",
    "lib": ["dom", "es2017"],
    "module": "commonjs",
    "moduleResolution": "node",
    "noEmit": true,
    "noFallthroughCasesInSwitch": true,
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "removeComments": true,
    "resolveJsonModule": true,
    "sourceMap": true,
    "strict": true,
    "target": "esnext"
  },
  "exclude": ["node_modules"]
}

我收到以下 Typescript 错误:

Cannot invoke an expression whose type lacks a call signature. Type 'typeof import(".../node_modules/next/types/index")' has no compatible call signatures.

我不明白这是怎么回事。任何人都可以帮助解释这个错误以及为什么这个流行包的核心功能之一抛出它吗?我是否需要手动扩展一些 Next 类型以弥补缺失的内容?

因为您的代码实际上适用于 next v8.1.0(最新发布的版本)和 @types/next v8.0.0,但与 next v8.1.1-canary.42 不兼容,这可能只是意味着他们引入了一些不兼容的更改,使现有的 .d.ts 文件已过时。 @types/* 包由独立志愿者维护,用于那些不费心发布 TypeScript 类型定义的包,并且经常更新较晚,尤其是当我们谈论某些 pre-released 版本时。

可以考虑使用最新版本的next(v8.1.0)吗?

作为替代方案,您可以尝试了解版本之间的变化,并生成您自己的 .d.ts 文件以供使用。