Strict Compiler Option in Typescript

The Strict compiler option enforces the strict type checking in TypeScript by enabling all the strict type rules under the strict family. The helps us to enable strict type checking without having to enable each compiler option separately. Note that future Typescript versions may introduce additional stricter checking options & enable them by default under this flag.

Enabling the Strict Compiler Option

You can enable the strict options in tsconfig.ts as shown below.

Enabling the strict option enables the following compiler rules (known as strict mode family options).

  1. alwaysStrict
  2. strictBindCallApply
  3. strictFunctionTypes
  4. strictNullChecks
  5. strictPropertyInitialization
  6. useUnknownInCatchVariables
  7. noImplicitAny
  8. noImplicitThis

The Future TypeScript versions may add additional type checking options to the above list. Hence they are automatically enabled by default. This is a good thing, but beware it may also break your code.

Opting Out of Certain Options

You may want to opt out of some of the above compiler rules. In that case, you can disable them by adding those flags in the tsconfig file

For Example, the following tsconfig option enables all the stricter options except noImplicitThis.

You can also use the ts-ignore to ignore the error for a specific line of code.

For Example, the following throws Parameter ‘n’ implicitly has an ‘any’ type (noImplicitAny) error.

Adding the // @ts-ignore above the line where the error is thrown will stop the TypeScript compiler from throwing the error.

Reference

— strict

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top