Search code examples
node.jstypescriptintellij-ideawebstorm

IntelliJ IDEA erroneously thinks MouseEvent interface is already declared


What is TS2687:All declarations of 'x' must have identical modifiers. ? I'm going through TS documentation to learn the language and following code from Function Parameter Bivariance section doesn't work for me

interface Event {
    timestamp: number;
}

interface MouseEvent extends Event {
    x: number;
    y: number
}

Renaming variables helps. I suspect MouseEvent is already defined somewhere but if so I can't understand where it's defined and what to do about it. It seems like it's only IntelliJ IDEA sees an error here but execution of single-file project by npm works correctly.

My config looks like this

{
  "compilerOptions": {
    "sourceMap": true,
    "experimentalDecorators": true,
    "strict": true,
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "noImplicitThis": true,
     "noImplicitReturns": true,             /* Report error when not all code paths in function return a value. */
     "noFallthroughCasesInSwitch": true     /* Report errors for fallthrough cases in switch statement. */
  },
  "exclude": [
    "node_modules"
  ]
}

Solution

  • In Typescript, an interface can be declared multiple times, so long as the declarations do not interfere with one another or a class with the same name. The error you're receiving is issued when certain property declarations are interfering with each other, specifically with regards to their modifiers.

    For instance, you would receive the same error for this code:

    class Thing {
        protected timestamp: number; // TS2687
    }
    
    // interfaces with the same name as a class effectively extend the class's definition.
    interface Thing {
        timestamp: number; // TS2687
    }
    

    It would appear that either Event or MouseEvent has already been declared in the context which are are trying to declare it, and that either timestamp or x or y have been declared non-public on those other declarations (specifically, class declarations).

    I don't have enough information to tell you if this is your exact problem.

    • Check to see if Event or MouseEvent are declared elsewhere in your project or one of its dependencies, and whether your interface's modifiers interfere with any existing modifiers on those classes.