
The --strict mode
TypeScript allows us to use the --strict compilation flag to enable all strict type checking options. Enabling --strict enables --noImplicitAny, --noImplicitThis, --alwaysStrict, --strictPropertyInitialization, and --strictNullChecks:
- The --strictNullChecks compilation flag enables non-nullable types.
- The --noImplicitAny flag forces us to explicitly declare the type of a variable when the type inference system is not able to automatically infer the correct type.
- The --alwaysStrict flag forces the TypeScript parse to use the strict mode.
- The --noImplicitThis flag forces us to explicitly declare the type of the this operator in functions when the type inference system is not able to automatically infer the correct type.
- The --strictPropertyInitialization flag forces class properties to be initialized.
Using the --strict compilation flag makes the TypeScript compiler much stricter. Enabling this option in existing large TypeScript projects can lead to the discovery of many errors that may require a significant effort to fix. For this reason, it is recommended to enable the --strict compilation flag in greenfield TypeScript projects and enable the individual flags (--noImplicitAny, --noImplicitThis, --alwaysStrict, and --strictNullChecks) progressively in existing TypeScript projects.
The --noImplicitReturns compilation flag is not one of the flags enabled by the --strict flag. The flag throws an error when not all code paths in function return a value. It is also recommended that you enable this flag in greenfield TypeScript projects or when possible on existing projects.