IntelliJ IDEA 错误地认为 MouseEvent 接口已经被声明

IntelliJ IDEA erroneously thinks MouseEvent interface is already declared

什么是TS2687:All declarations of 'x' must have identical modifiers.?我正在阅读 TS 文档来学习语言,Function Parameter Bivariance 部分的以下代码对我不起作用

interface Event {
    timestamp: number;
}

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

重命名变量有帮助。我怀疑 MouseEvent 已经在某处定义,但如果是这样,我无法理解它的定义位置以及如何处理它。似乎只有 IntelliJ IDEA 在这里看到错误,但是 npm 执行单文件项目可以正常工作。

我的配置是这样的

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

在 Typescript 中,可以多次声明一个接口,只要这些声明不会相互干扰或 class 同名。您收到的错误是在某些 属性 声明相互干扰时发出的,特别是关于它们的修饰符。

例如,对于此代码,您会收到相同的错误:

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
}

似乎 EventMouseEvent 已经在试图声明它的上下文中声明,并且 timestampxy 在那些其他声明中被声明为非 public(具体来说,class 声明)。

我没有足够的信息来判断这是否正是您的问题。

  • 检查 EventMouseEvent 是否在您的项目或其依赖项之一的其他地方声明,以及您的界面修饰符是否会干扰那些 class 上的任何现有修饰符.