Angular 10 更严格的设置 --strict

Angular 10 Stricter Settings --strict

在 Angular 10 中,您可以使用 ng new --strict

创建一个新项目

Enabling this flag initializes your new project with a few new settings that improve maintainability, help you catch bugs ahead of time, and allow the CLI to perform advanced optimizations on your app

是否有命令行可以将现有项目升级到严格模式,或者我只需要创建一个新项目然后从其他项目复制粘贴文件?

基本上有几件事发生了变化:

  1. tsconfig.base.json 获得一些新规则:
"compilerOptions": {
  "strict": true,
  "forceConsistentCasingInFileNames": true,
  "noFallthroughCasesInSwitch": true
},
"angularCompilerOptions": {
  "strictInjectionParameters": true,
  "strictTemplates": true
}   
  1. tslint.json 得到更新
"no-any": true
  1. 您的 angular.json:
  2. 中的捆绑预算大小已减少
"configurations": {
  //...
  [envName]: {
    "budgets": [
      {
        "type": "initial",
        "maximumWarning": "500kb",
        "maximumError": "1mb",
      },
      {
        "type": "anyComponentStyle",
        "maximumWarning": "2kb",
        "maximumError": "4kb",
      },
    ]
  }
}

并在 angular.json 中的 projects.[projectName].schematics 路径中添加了示意图:

schematics: {
  "@schematics/angular:application": {
    "strict": true
  }
}
  1. 应用 package.json 更新 sideEffects 属性:
sideEffects: false

为了解释这是做什么的,我可以向您推荐 答案。

package.json 位于您的应用程序文件夹中。在使用 ng update 选项时,应该将其添加到迁移中。如果你没有这个文件,你可以使用这个 template

要转​​换当前的代码库,尤其是 tsconfig 更新和 no-any 修复起来会让您头疼。但是获得更好(更少隐藏的错误)和更容易重构代码库绝对是值得的。