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

Custom TypeScript type definitions file is ignored

我正在做一个 Angular 9 项目。我想在我的项目中使用 ResizeObserver, but since this type is currently missing from TypeScript's lib.dom.d.ts (issue), I tried to add Jason Strothmann's ResizeObserver.d.ts 文件。

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

  1. 我在 src 下创建了一个 types 目录。
  2. 我将ResizeObserver.d.ts复制到src/types
  3. 我将 "./src/types" 添加到 tsconfig.json 中的 "typeRoots" 数组(它在 app 文件夹中,就像 src 一样)。
  4. 我在 src/components/foo/foo.component.ts 中声明了一个 ResizeObserver 类型的对象,但 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"]
}

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

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

"include": ["./src/types/*.d.ts"]