javascript - NextJS 服务器文件中的 typescript 错误

标签 javascript typescript next.js

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

  • 下一个 v8.1.1-canary.42
  • @types/next v8.0.5

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 类型定义,并且经常更新较晚,特别是在我们谈论某些预发布版本时。

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

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

关于javascript - NextJS 服务器文件中的 typescript 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56398285/

相关文章:

javascript - 从 ajax 结果中提取 XML 数据

javascript - 在 loopback 4 中创建 requestBody 的最佳实践是什么?

next.js - Next 图像组件在生产中抛出 404 错误,在开发中工作正常

javascript - 错误 : Hydration failed because the initial UI does not match what was rendered on the server with useSession() and react-bootstrap

reactjs - Firebase Firestore 模拟器错误 `Host has been set in both settings() and useEmulator(), emulator host will be used`

javascript - 从左到右动画 div

php - 即时 Assets 打包器

javascript - 如何在 javascript 中使用嵌套函数作为生成器(使用 "inner"产量)

typescript - 在哪里可以找到 TypeScript 的内置类型和标准库的文档?

Angular 6 - 无法解析 AppComponent 的所有参数