自定义 TypeScript 类型定义文件被忽略

kol*_*kol 8 typescript tsconfig visual-studio-code type-definition angular

我正在开发 Angular 9 项目。我想使用ResizeObserver,但由于 TypeScript 的 lib.dom.d.ts 目前缺少此类型(问题),因此我尝试将 Jason Strothmann 的ResizeObserver.d.ts文件添加到我的项目中。

我遵循了此页面给出的建议:

  1. types在 下创建了一个目录src
  2. 我复制ResizeObserver.d.tssrc/types.
  3. 我添加"./src/types""typeRoots"数组中tsconfig.json(它位于app文件夹中,就像src)。
  4. ResizeObserver我在 中声明了一个类型的对象src/components/foo/foo.component.ts,但 TypeScript 无法识别该类型,即使在停止并重新启动ng serve或重新启动 VS Code 后也是如此。

我的tsconfig.json看起来像这样:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./out-tsc",
    "sourceMap": true,
    "declaration": false,
    "importHelpers": true,
    "experimentalDecorators": true,
    "moduleResolution": "Node",
    "module": "ES2015",
    "target": "ES2015",
    "typeRoots": ["./src/types", "./node_modules/@types"],
    "lib": ["ES2015", "DOM", "DOM.Iterable"]
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  },
  "files": ["./src/main.ts"]
}
Run Code Online (Sandbox Code Playgroud)

所有其他(JavaScript、DOM 或 Angular 相关)类型均可被 TypeScript 识别,仅忽略中定义的类型ResizeObserver.d.ts。我究竟做错了什么?

kol*_*kol 8

我已经找到问题了。我必须将此行添加到tsconfig.json

"include": ["./src/types/*.d.ts"]
Run Code Online (Sandbox Code Playgroud)