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

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

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

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

  • 我创建了一个 types src下的目录.
  • 我复制了 ResizeObserver.d.ts进入 src/types .
  • 我加了 "./src/types""typeRoots" tsconfig.json 中的数组(它在 app 文件夹中,就像 src 一样)。
  • 我声明了一个 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"]
    }
    

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

    最佳答案

    我找到了问题所在。我不得不将此行添加到 tsconfig.json :

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

    关于angular - 自定义 TypeScript 类型定义文件被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61770231/

    相关文章:

    flutter - 当我按 Ctrl+S 时,为什么我的 Dart 代码会自动移动到不同的行?

    javascript - 无法运行 Node.js 应用程序,因为 : 'Failed to lookup view "error"in views directory'

    visual-studio-code - 带有 VScode 的 phpcs - 如何禁用行长警告?

    angular - 无法使用 moment 调用类型缺少调用签名的表达式

    在 axis.tickFormat() 中使用 d3.timeFormat 时出现 TypeScript 错误

    typescript - 为什么我的 Typescript 函数中缺少 void 隐式返回类型?

    javascript - 当我真正想要的时候,类型 'Object' 上不存在属性 - Angular2+

    laravel - 无法在 Gitlens 中看到文件历史记录(在特定项目上)

    预设值时,Angular 4 响应式(Reactive)表单验证器要求显示触摸错误

    angular - 在 iframe、Angular-8 中显示本地 HTML