'TypeScript error TS2403: Subsequent variable declarations must have the same type

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


Solution 1:[1]

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

Solution 2:[2]

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.

Solution 3:[3]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 jmoe
Solution 3 jjmerelo