global.d.ts 无效


解决办法

tsconfig.json

{
  "compilerOptions": {
    "typeRoots": ["./node_modules/@types/", "./custom/path/to/declarations/"]
  },
}

示例

例如,在根目录下新建 types 目录

types/global.d.ts

declare interface Window {
  opts: Object
}

tsconfig.json

{
  "compilerOptions": {
    "typeRoots": ["./node_modules/@types/", "./types/"]
  },
}

参考资料

  • https://stackoverflow.com/questions/57125019/create-react-app-typescript-global-d-ts-file-doesnt-work
  • https://www.typescriptlang.org/docs/handbook/tsconfig-json.html#types-typeroots-and-types