微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

打字稿的全局模块定义

如何解决打字稿的全局模块定义

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

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

解决方法

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。