【问题标题】:TypeScript error TS2403: Subsequent variable declarations must have the same typeTypeScript 错误 TS2403:后续变量声明必须具有相同的类型
【发布时间】:2019-09-04 22:00:02
【问题描述】:

我的 TypeScript 项目似乎遇到了一些编译错误。完整的错误是:

node_modules/@types/mocha/index.d.ts:2680:13 - error TS2403: Subsequent 
variable declarations must have the same type.  Variable 'beforeEach'
must be of type 'Lifecycle',  but here has type 'HookFunction'.

2680 declare var beforeEach: Mocha.HookFunction;
                 ~~~~~~~~~~

我有 7 个这样的错误都在同一个依赖项 (Mocha) 中。我正在使用 TypeScript ^3.3.3,这是我的 tsconfig.json:

{
  "compilerOptions": {
    "composite": false,
    "declaration": true,
    "declarationMap": true,
    "removeComments": true,

    "target": "es2017",
    "lib": ["dom", "es2015", "es2016", "es2017"],
    "module": "commonjs",
    "moduleResolution": "node",
    "esModuleInterop": true,
    "resolveJsonModule": true,

    "jsx": "preserve",
    "allowJs": false,
    "strict": true,
    "noFallthroughCasesInSwitch": true,
    "noImplicitReturns": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "sourceMap": true,
    "outDir": "build",
    "noUnusedParameters": true,

    "noUnusedLocals": false,

    "baseUrl": ".",
    "paths": {
      "*": ["./types/*"],
    },

    "rootDir": "./src",

    "typeRoots": ["./@types", "./node_modules/@types"]
  },

  "exclude": [
    "node_modules",
    "build",
    "dist",
    "__mocks__",
    "__tests__",
    "coverage",
    "*.config.js",
    "*.babel.js",
    "*.test.ts",
    "specs"
  ]
}

另外,这些是我的开发依赖项:

"devDependencies": {
  "@types/jest": "^24.0.9",
  "@types/koa": "^2.0.48",
  "@types/lodash": "^4.14.121",
  "@types/mocha": "^5.2.6",
  "@types/twig": "^1.12.2",
  "@types/uuid": "^3.4.4",
  "chai": "^4.1.2",
  "concurrently": "^4.1.0",
  "db-migrate": "^0.11.5",
  "dotenv": "^6.0.0",
  "grunt": "^1.0.3",
  "grunt-cli": "^1.2.0",
  "jest": "^23.1.0",
  "nodemon": "^1.17.2",
  "ts-jest": "^23.10.5",
  "ts-node": "^8.0.2",
  "tslint": "^5.14.0",
  "typescript": "^3.3.3"
}

这是我的编译命令:

tsc src/index.ts

【问题讨论】:

    标签: typescript compiler-errors mocha.js


    【解决方案1】:

    我在 tsconfig 文件中添加了以下属性

    "compilerOptions": {
        "skipLibCheck": true
    },
    

    这告诉 TypeScript 我们要跳过 node_modules 文件夹中库的类型检查。这可以节省编译时间并阻止节点模块中的冲突类型破坏构建。 对于那些想要解释为什么您可能需要此选项的人,这里是资源链接https://www.typescriptlang.org/tsconfig#skipLibCheck

    【讨论】:

    • 这是黄金。谢谢
    • 这行得通。但是有什么摩擦吗?为什么会这样?
    【解决方案2】:

    看起来@types/mocha@types/jest 有类似的声明。所以如果两者都有,卸载@types/mocha:

    npm uninstall @types/mocha.

    这为我解决了这个问题。

    【讨论】:

    • 这可以解释它,但它也不是一个解决方案。在我的项目中,我使用mocha,然后安装了另一个间接使用jest 的依赖项。这导致了两者之间的冲突和编译失败。我不能(不会)简单地停止使用 mocha 来解决这个问题。
    【解决方案3】:

    您可以将@types/mocha 替换为@types/jest

    【讨论】:

    • 他两者都有(?)
    • 为什么这是一个答案,为什么它得到任何赞成?
    • 看起来 mocha 和 jest 都在每个钩子之前声明,这可能是错误的原因。
    猜你喜欢
    • 1970-01-01
    • 2016-11-07
    • 1970-01-01
    • 2020-01-06
    • 2017-11-15
    • 2018-04-18
    • 1970-01-01
    • 2020-01-03
    • 2022-09-30
    相关资源
    最近更新 更多