【发布时间】:2020-08-29 09:16:26
【问题描述】:
我正在开发一个 Angular 9 项目。我想使用 ResizeObserver,但由于 TypeScript 的 lib.dom.d.ts (issue) 目前缺少这种类型,因此我尝试将 Jason Strothmann 的 ResizeObserver.d.ts 文件添加到我的项目中。
我听从了this页面给出的建议:
- 我在
src下创建了一个types目录。 - 我将
ResizeObserver.d.ts复制到src/types。 - 我在
tsconfig.json的"typeRoots"数组中添加了"./src/types"(它在app文件夹中,就像src)。 - 我在
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