【问题标题】:New Angular project won't compile with strict mode新的 Angular 项目不会在严格模式下编译
【发布时间】: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


【解决方案1】:

只需注释掉或从 main.ts 中删除以下行(由升级过程添加):

export { renderModule, renderModuleFactory } from '@angular/platform-server';

可能会解决您的问题

【讨论】:

    【解决方案2】:

    在严格模式下赋值之前声明变量:

    var title = "portal"; 
    

    let title = "portal"
    

    【讨论】:

    • 这个答案与问题无关。
    【解决方案3】:

    试试这个:

    1:删除节点模块

    2:运行 npm install

    3:运行 ng serve --aot=false

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-13
      • 2022-09-25
      • 2018-10-22
      • 2013-03-30
      • 1970-01-01
      • 2019-12-25
      相关资源
      最近更新 更多