【问题标题】:Custom TypeScript type definitions file is ignored自定义 TypeScript 类型定义文件被忽略
【发布时间】:2020-08-29 09:16:26
【问题描述】:

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

我听从了this页面给出的建议:

  1. 我在src下创建了一个types目录。
  2. 我将ResizeObserver.d.ts 复制到src/types
  3. 我在tsconfig.json"typeRoots" 数组中添加了"./src/types"(它在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"]
}

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

【问题讨论】:

    标签: angular typescript visual-studio-code tsconfig type-definition


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2020-09-27
      • 1970-01-01
      • 1970-01-01
      • 2010-09-10
      • 2023-03-14
      • 2014-10-09
      • 2013-10-10
      • 1970-01-01
      • 2021-07-31
      相关资源
      最近更新 更多