首页 > 解决方案 > 自定义 TypeScript 类型定义文件被忽略

问题描述

我正在做一个 Angular 9 项目。我想使用ResizeObserver,但由于 TypeScript 的 lib.dom.d.ts ( issue ) 目前缺少这种类型,我尝试将 Jason Strothmann 的ResizeObserver.d.ts文件添加到我的项目中。

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

  1. typessrc.
  2. 我复制ResizeObserver.d.tssrc/types.
  3. 我添加"./src/types""typeRoots"数组中tsconfig.json(它在app文件夹中,就像src)。
  4. 我声明了一个 in 类型的对象ResizeObserversrc/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"]
}

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

标签: angulartypescriptvisual-studio-codetsconfigtype-definition

解决方案


我发现了问题。我不得不将此行添加到tsconfig.json

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

推荐阅读