TypeScript error TS2403: Subsequent variable declarations must have the same type
Asked Answered
C

5

45

I seem to be running into some compile errors on my TypeScript project. The full error is:

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;
                 ~~~~~~~~~~

I have 7 of these errors all in the same dependency (Mocha). I'm using TypeScript ^3.3.3 and this my 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"
  ]
}

Also, these are my dev dependencies:

"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"
}

And this is my compile command:

tsc src/index.ts
Continuation answered 14/4, 2019 at 21:57 Comment(0)
C
75

I added the following property to the tsconfig file

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

This tells TypeScript that we want to skip the type checking of libraries in the node_modules folder. This saves compilation time and stops conflicting types in node modules from breaking the build. For those who want some explanation on why you might need this option here is a resource link https://www.typescriptlang.org/tsconfig#skipLibCheck

Commemoration answered 4/1, 2021 at 19:13 Comment(3)
This is gold. ThanksTepid
This worked. But is there any rub? Why does this work?Ophthalmitis
Probably a better idea to not disable typechecking for all external libraries. See @jmoe's answer below.Stimulative
T
32

Looks like @types/mocha and @types/jest have similar declarations. So if you have both, uninstall @types/mocha:

npm uninstall @types/mocha.

This resolved the issue for me.

Touraine answered 5/10, 2020 at 4:24 Comment(1)
This may explain it but it's not a solution either. In my project I'm using mocha and then installed another dependency which indirectly used jest. This caused conflicts between the two and failed compilation. I can't (won't) simply stop using mocha to resolve this.Terrazas
O
1

TL;DR: No, you can't put together mocha (and other test runners that use mocha, such as web-test-runner) together in the same module.

Types can be defined only once, and mocha and jest declare a series of globals (needed so that they can be used directly without importing) which are incompatible with each other. You need to commit to one or the other, or if you use things such as web-test-runner or electron-mocha just choose another runner (cypress will do, for instance).

Any workaround will hide one or the other, so eventually you can't use them together. At the end of the day, it's probably not a good idea to declare two test runners as dependencies, so you might as well refactor the code to go for one or the other.

Ophthalmitis answered 19/4, 2022 at 5:46 Comment(0)
M
0

In my case I am using Jest for my unit tests but a library used by integration tests in our pipeline is transitively installing @types/mocha. I don't need the Mocha types so I created a mocha.d.ts file and just added

declare module 'mocha' {}

which overrides the Mocha types without having to uninstall anything or skip lib checks.

Malonylurea answered 18/4 at 20:50 Comment(0)
G
-4

you can replace @types/mocha with @types/jest

Gradin answered 23/6, 2019 at 13:29 Comment(3)
He has both (?)Allheal
Why is this an answer and why has it got any upvotes?Fiddlefaddle
Looks like both mocha and jest declare beforeEach hook, that might be the reason for error.Salify

© 2022 - 2024 — McMap. All rights reserved.