2

我正在使用以下文件结构在 Webstorm 中开发一个项目

| src
    | ...
    | many files
| types
    | SomeInterface
        | index.d.ts
    | AnotherInterface
        | index.d.ts
    | index.d.ts

我想让SomeInterface全球可用(使用接口而不需要导入)。

我的主要index.d.ts内容如下:

import { SomeInterface } from './SomeInterface'
import { AnotherInterface} from './AnotherInterface'

declare global {
  interface MainInterface extends SomeInterface {
    someAttribute?: number[]
    names: SomeInterface[]
    contactData: AnotherInterface[]
  }
}

每当我尝试在以下位置实现类型定义时src/some/path/to/file.ts

function someFunctionName(parsedData: MainInterface):void{...}

我收到以下消息: ESLint: 'MainInterface' is not defined.(no-undef)

这是我当前的 tsconfig.json

{
  "compilerOptions": {
    "target": "esnext",
    "allowJs": true,
    "checkJs": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "noImplicitAny": false,
    "baseUrl": "./",
    "paths": {
      "*": ["src/*"]
    }
  },
  "include": ["src", "types/index.d.ts"],
  "exclude": ["node_modules"]
}

我错过了什么吗?我知道不建议让东西在全球范围内可用,但是我被要求以这种方式专门这样做。

4

2 回答 2

5

如果您在代码中定义自定义全局变量,则需要告诉 ESLint:https ://eslint.org/docs/user-guide/configuring#specifying-globals

不过,我只是考虑关闭no-undef规则,因为它是 TypeScript 本身已经涵盖的检查。

于 2020-08-20T22:39:59.813 回答
2

typescript -eslint FAQ在这里提供了指导:

我们强烈建议您不要在 TypeScript 项目中使用 no-undef lint 规则。它提供的检查已经由 TypeScript 提供,无需配置 - TypeScript 只是在这方面做得更好。

但是no-undef对 JavaScript 很有用,所以如果你混合使用,最好只对使用 TypeScript 的文件禁用它,例如 Vue + TypeScript:

"overrides": [
    {
        "files": ["*.ts", "*.vue"],
        "rules": {
            "no-undef": "off"
        }
    }
]

当然不要使用 JavaScript 编写 Vue 文件,因为no-undef它被禁用了。

于 2021-09-12T23:26:55.887 回答