cypress: tsc type definition conflicts, duplicate identifiers in @types/mocha chai etc

Project is https://github.com/ericblade/quagga2

Project has it’s own dependency on @types/mocha @types/chai declared. This is so editor support for typescript works correctly.

Running tsc on the project in the build step fails with duplicate definitions.

On top of that, since this is also causing typescript compilation to fail, when I run ‘cypress run’, which uses webpack-preprocessor to package, cypress just hangs indefinitely, when I would expect it to fail.

Running cypress open does not fail, as for speed reasons, I have configured open to not use webpack-preprocessor.

Reverting to 4.5.0 corrects the issues, and all tests run as normal.

Version of webpack-preprocessor doesn’t seem relevant – I tried both 5.2.1 which is what I used prior to the 4.6.0 upgrade, and 5.4.1, which was the recommended upgrade with cypress 4.6.0.

> @ericblade/quagga2@1.0.5 check-types D:\src\quaggajs
> tsc

node_modules/@types/chai/index.d.ts:19:1 - error TS6200: Definitions of the following identifiers conflict with those in another file: Message, ObjectProperty, ChaiPlugin, AssertionArgs, Operator, OperatorComparable, AssertionError, chai

19 declare namespace Chai {
   ~~~~~~~

  node_modules/cypress/types/chai/index.d.ts:19:1
    19 declare namespace Chai {
       ~~~~~~~
    Conflicts are in this file.

node_modules/@types/mocha/index.d.ts:17:15 - error TS2300: Duplicate identifier 'Mocha'.

17 declare class Mocha {
                 ~~~~~

  node_modules/cypress/types/mocha/index.d.ts:17:15
    17 declare class Mocha {
                     ~~~~~
    'Mocha' was also declared here.
  node_modules/cypress/types/mocha/index.d.ts:242:19
    242 declare namespace Mocha {
                          ~~~~~
    and here.
  node_modules/cypress/types/mocha/index.d.ts:2856:14
    2856     export = Mocha;
                      ~~~~~
    and here.

node_modules/@types/mocha/index.d.ts:242:19 - error TS2300: Duplicate identifier 'Mocha'.      

242 declare namespace Mocha {
                      ~~~~~

  node_modules/cypress/types/mocha/index.d.ts:17:15
    17 declare class Mocha {
                     ~~~~~
    'Mocha' was also declared here.
  node_modules/cypress/types/mocha/index.d.ts:242:19
    242 declare namespace Mocha {
                          ~~~~~
    and here.
  node_modules/cypress/types/mocha/index.d.ts:2856:14
    2856     export = Mocha;
                      ~~~~~
    and here.

node_modules/@types/mocha/index.d.ts:2814:15 - error TS2451: Cannot redeclare block-scoped variable 'mocha'.

2814 declare const mocha: BrowserMocha;
                   ~~~~~

  node_modules/cypress/types/mocha/index.d.ts:2814:15
    2814 declare const mocha: BrowserMocha;
                       ~~~~~
    'mocha' was also declared here.

node_modules/@types/mocha/index.d.ts:2848:6 - error TS2300: Duplicate identifier 'MochaDone'.  

2848 type MochaDone = Mocha.Done;
          ~~~~~~~~~

  node_modules/cypress/types/mocha/index.d.ts:2848:6
    2848 type MochaDone = Mocha.Done;
              ~~~~~~~~~
    'MochaDone' was also declared here.

node_modules/@types/mocha/index.d.ts:2851:6 - error TS2300: Duplicate identifier 'ReporterConstructor'.

2851 type ReporterConstructor = Mocha.ReporterConstructor;
          ~~~~~~~~~~~~~~~~~~~

  node_modules/cypress/types/mocha/index.d.ts:2851:6
    2851 type ReporterConstructor = Mocha.ReporterConstructor;
              ~~~~~~~~~~~~~~~~~~~
    'ReporterConstructor' was also declared here.

node_modules/@types/mocha/index.d.ts:2856:14 - error TS2300: Duplicate identifier 'Mocha'.     

2856     export = Mocha;
                  ~~~~~

  node_modules/cypress/types/mocha/index.d.ts:17:15
    17 declare class Mocha {
                     ~~~~~
    'Mocha' was also declared here.
  node_modules/cypress/types/mocha/index.d.ts:242:19
    242 declare namespace Mocha {
                          ~~~~~
    and here.
  node_modules/cypress/types/mocha/index.d.ts:2856:14
    2856     export = Mocha;
                      ~~~~~
    and here.

node_modules/@types/mocha/index.d.ts:2860:14 - error TS2300: Duplicate identifier 'milliseconds'.

2860     export = milliseconds;
                  ~~~~~~~~~~~~

  node_modules/cypress/types/mocha/index.d.ts:2860:14
    2860     export = milliseconds;
                      ~~~~~~~~~~~~
    'milliseconds' was also declared here.

node_modules/@types/mocha/index.d.ts:2879:14 - error TS2300: Duplicate identifier 'common'.    

2879     export = common;
                  ~~~~~~

  node_modules/cypress/types/mocha/index.d.ts:2879:14
    2879     export = common;
                      ~~~~~~
    'common' was also declared here.

node_modules/cypress/types/chai/index.d.ts:19:1 - error TS6200: Definitions of the following identifiers conflict with those in another file: Message, ObjectProperty, ChaiPlugin, AssertionArgs, Operator, OperatorComparable, AssertionError, chai

19 declare namespace Chai {
   ~~~~~~~

  node_modules/@types/chai/index.d.ts:19:1
    19 declare namespace Chai {
       ~~~~~~~
    Conflicts are in this file.

node_modules/cypress/types/mocha/index.d.ts:17:15 - error TS2300: Duplicate identifier 'Mocha'.
17 declare class Mocha {
                 ~~~~~

  node_modules/@types/mocha/index.d.ts:17:15
    17 declare class Mocha {
                     ~~~~~
    'Mocha' was also declared here.
  node_modules/@types/mocha/index.d.ts:242:19
    242 declare namespace Mocha {
                          ~~~~~
    and here.
  node_modules/@types/mocha/index.d.ts:2856:14
    2856     export = Mocha;
                      ~~~~~
    and here.

node_modules/cypress/types/mocha/index.d.ts:242:19 - error TS2300: Duplicate identifier 'Mocha'.

242 declare namespace Mocha {
                      ~~~~~

  node_modules/@types/mocha/index.d.ts:17:15
    17 declare class Mocha {
                     ~~~~~
    'Mocha' was also declared here.
  node_modules/@types/mocha/index.d.ts:242:19
    242 declare namespace Mocha {
                          ~~~~~
    and here.
  node_modules/@types/mocha/index.d.ts:2856:14
    2856     export = Mocha;
                      ~~~~~
    and here.

node_modules/cypress/types/mocha/index.d.ts:2814:15 - error TS2451: Cannot redeclare block-scoped variable 'mocha'.

2814 declare const mocha: BrowserMocha;
                   ~~~~~

  node_modules/@types/mocha/index.d.ts:2814:15
    2814 declare const mocha: BrowserMocha;
                       ~~~~~
    'mocha' was also declared here.

node_modules/cypress/types/mocha/index.d.ts:2848:6 - error TS2300: Duplicate identifier 'MochaDone'.

2848 type MochaDone = Mocha.Done;
          ~~~~~~~~~

  node_modules/@types/mocha/index.d.ts:2848:6
    2848 type MochaDone = Mocha.Done;
              ~~~~~~~~~
    'MochaDone' was also declared here.

node_modules/cypress/types/mocha/index.d.ts:2851:6 - error TS2300: Duplicate identifier 'ReporterConstructor'.

2851 type ReporterConstructor = Mocha.ReporterConstructor;
          ~~~~~~~~~~~~~~~~~~~

  node_modules/@types/mocha/index.d.ts:2851:6
    2851 type ReporterConstructor = Mocha.ReporterConstructor;
              ~~~~~~~~~~~~~~~~~~~
    'ReporterConstructor' was also declared here.

node_modules/cypress/types/mocha/index.d.ts:2856:14 - error TS2300: Duplicate identifier 'Mocha'.

2856     export = Mocha;
                  ~~~~~

  node_modules/@types/mocha/index.d.ts:17:15
    17 declare class Mocha {
                     ~~~~~
    'Mocha' was also declared here.
  node_modules/@types/mocha/index.d.ts:242:19
    242 declare namespace Mocha {
                          ~~~~~
    and here.
  node_modules/@types/mocha/index.d.ts:2856:14
    2856     export = Mocha;
                      ~~~~~
    and here.

node_modules/cypress/types/mocha/index.d.ts:2860:14 - error TS2300: Duplicate identifier 'milliseconds'.

2860     export = milliseconds;
                  ~~~~~~~~~~~~

  node_modules/@types/mocha/index.d.ts:2860:14
    2860     export = milliseconds;
                      ~~~~~~~~~~~~
    'milliseconds' was also declared here.

node_modules/cypress/types/mocha/index.d.ts:2879:14 - error TS2300: Duplicate identifier 'common'.

2879     export = common;
                  ~~~~~~

  node_modules/@types/mocha/index.d.ts:2879:14
    2879     export = common;
                      ~~~~~~
    'common' was also declared here.


Found 18 errors.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 5
  • Comments: 24 (4 by maintainers)

Most upvoted comments

My workaround for chai-subset is to uninstall @types/chai-subset (which depends on @types/chai), and to instead place a modified version of the chai-subset type definitions in my project. I put this file in cypress/types/chai-subset.d.ts:

/**
 * This is copied from `@types/chai-subset`. The types package imports
 * `@types/chai` which conflicts with Cypress' definitions for chai which is why
 * this code is copied instead of installing that package.
 */

declare module "chai-subset" {
  global {
    namespace Chai {
      interface Assertion {
        containSubset(expected: any): Assertion
      }
      interface Assert {
        containSubset(val: any, exp: any, msg?: string): void
      }
    }
  }

  const chaiSubset: Chai.ChaiPlugin
  export = chaiSubset
}

I’m not sure it’s really necessary for @types/chai-subset to depend on @types/chai. If it didn’t have that dependency, and did not include this line then I think that would solve the problem for me.

/// <reference types="chai" />

@jrnail23 what worked for me was putting a separate tsconfig in the cypress directory, which lists cypress in it’s types config, and removing cypress from the tsconfig in the root directory.

I still have suspicion that the entire methodology going on here might have some flaws, that that doesn’t address though, but no one has addressed my suspicions above so far.

Hey @ericblade - we’re still sorting out the best practices for ensuring globals in type definitions among various dependencies don’t conflict.

Can you try adding a tsconfig.json inside the cypress folder with:

{
   "extends": "../tsconfig.json",
   "compilerOptions": {
      "types": ["cypress"]
   }
}

and in your base tsconfig.json remove the "cypress" from the "types" dependencies array

"types": [
  "react",
  "mocha",
] /* Type declaration files to be included in compilation. */,

and see if that helps with the issues you’re describing?