我正在使用以下软件包版本:
- 下一个 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/