打字稿的全局模块定义

时间:2020-08-20 17:05:03

标签: typescript eslint typescript-typings typescript-eslint

我正在使用以下文件结构在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"]
}

我错过了什么吗?我知道不建议在全球范围内使用,但有人特别要求我这样做。

1 个答案:

答案 0 :(得分:2)

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

尽管如此,我还是考虑关闭no-undef规则,因为TypeScript本身已经涵盖了这项检查。