In Angular 10 you can create a new project using 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
Is there a command line that will upgrade an existing project to strict mode, or I just need to create a new project and then copy-paste the files from the other project?
To enable strict-mode, just add strict: true in your tsconfig. json file. It enables all flags which makes its compiler more strict.
Disable strict checks entirely by setting strictTemplates: false in the application's TypeScript configuration file, tsconfig. json. Disable certain type-checking operations individually, while maintaining strictness in other aspects, by setting a strictness flag to false.
Strict mode improves maintainability and helps you catch bugs ahead of time. Additionally, strict mode applications are easier to statically analyze and can help the ng update command refactor code more safely and precisely when you are updating to future versions of Angular.
Enabling Angular strict mode through the Angular CLI will enable TypeScript strict mode in your app also as enable some Angular strict checks. Angular Strict template checking is enabled via strictTemplates in tsconfig. json . It'll force the Angular compiler to check tons of things.
Basically a couple things change:
tsconfig.base.json
gets some new rules:"compilerOptions": { "strict": true, "forceConsistentCasingInFileNames": true, "noFallthroughCasesInSwitch": true }, "angularCompilerOptions": { "strictInjectionParameters": true, "strictTemplates": true }
tslint.json
gets updated"no-any": true
angular.json
:"configurations": { //... [envName]: { "budgets": [ { "type": "initial", "maximumWarning": "500kb", "maximumError": "1mb", }, { "type": "anyComponentStyle", "maximumWarning": "2kb", "maximumError": "4kb", }, ] } }
and a schematics addition to your projects.[projectName].schematics
path in the angular.json
:
schematics: { "@schematics/angular:application": { "strict": true } }
package.json
gets updated with a sideEffects
property:sideEffects: false
For an explanation what this does, I can refer you to this answer.
This package.json
is located inside your app folder. It should be added there with the migration while using the ng update
option. If you do not have this file, you can use this template
To convert a current codebase, especially the tsconfig updates and the no-any
will give you some headache to fix. But it will be definitely worth it to get a better (less hidden bugs) and easier to refactor codebase.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With