【问题标题】:Typescript error in server file for NextJSNextJS 的服务器文件中的打字稿错误
【发布时间】:2019-06-01 00:37:49
【问题描述】:

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

  • 下一个 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 类型?

【问题讨论】:

    标签: javascript typescript next.js


    【解决方案1】:

    由于您的代码实际上适用于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 文件以供使用。

    【讨论】:

    • 你是对的!我领先于游戏,因为我说 Typescript 声明已添加到 NextJS 的预发布版本中,我认为这会有所帮助,因为我正在将 Typescript 添加到我的 NextJS 项目中,但在恢复到当前版本后,它已被删除该错误加上我遇到的其他 3 个错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 2021-10-23
    • 2021-01-02
    • 2019-10-15
    • 2020-01-21
    • 1970-01-01
    • 2017-11-28
    相关资源
    最近更新 更多