【发布时间】:2018-09-09 15:24:50
【问题描述】:
使用 CLI 生成 Angular 项目
修改tsconfig,增加严格选项
{
"compileOnSave": false,
"compilerOptions": {
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"strict": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"typeRoots": ["node_modules/@types"],
"lib": ["es2017", "dom"]
}
}
运行ng serve 时出现以下错误:
src/app/app.component.ts(2,29) 中的错误:错误 TS7016:找不到 模块“util”的声明文件。 'D:/proj/node_modules/util/util.js' 隐含地具有“任何”类型。试试
npm install @types/util存在或添加包含declare module 'util';的新声明(.d.ts)文件
我试过npm install 建议-这个模块不存在。
我还尝试删除 node_modules 并执行npm install。一样。有什么指点吗?
编辑
app.component.ts
import { Component, OnInit } from "@angular/core";
import { isUndefined } from "util";
@Component({
selector: "app-root",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.scss"]
})
export class AppComponent {
title = "Portal";
}
【问题讨论】:
-
此代码不是由 Angular CLI 生成的。因此,您或您团队中的某个人选择添加未使用的
isUndefined导入。只需将其删除。 -
好吧,
strict暗示noImplicitAny,而这个util库可能不在 TypeScript 中并且没有定义文件。要么生成定义文件,要么删除它并使用typeof variable === "undefined",而不是依赖第三方库。 -
@JBNizet 谢谢,我没有添加那行,但我确实玩了一些代码,所以 VSCode 有可能自动添加它吗?
-
什么版本的 CLI?我也会删除“isUndefined”的导入
-
我也有类似的问题,我用的是
"noImplicitAny": true,它带有"strict": true和"skipLibCheck": true,它应该忽略@types文件夹中的问题,但它没有。跨度>
标签: angular typescript