The --strict Compiler Option in TypeScript

June 9, 2017

TypeScript 2.3 introduced a new --strict compiler option that enables a number of other compiler options related to stricter type checking.

Strict Type Checking Options

The idea is that you opt into a strict-by-default mode so that you enjoy all the benefits of better type safety without having to enable each compiler option separately. You can either set the --strict flag on the command line or specify the strict option within your project's tsconfig.json file to opt into this mode.

As of TypeScript 2.3 in early June 2017, the --strict flag enables the following four compiler options:

--strictNullChecks

--noImplicitAny

--noImplicitThis

--alwaysStrict

Future versions of TypeScript might add additional type checking options to this set. This means you don't necessarily have to monitor each TypeScript release for new strictness options that you should enable in your project. If new options are added to the option set above, they'll be active automatically once you upgrade your project's TypeScript version.

Opting Out of Certain Options

Every option enabled by --strict can be configured independently. That is, you can still override all the defaults without having to give up defaulting to strict mode.

An example: Let's say you want all strict type checking options enabled except for --alwaysStrict which makes the compiler parse all source files in strict mode and emit "use strict"; directives. In that case, you can specify the following options in your tsconfig.json configuration file:

{
"strict": true,
"alwaysStrict": false
}

This configuration works as you would expect and enables all strict type checking options except --alwaysStrict. You'll still automatically benefit from new strictness options as they're added in the future.

Scaffolding a Configuration File with tsc --init

To help you get started with a new TypeScript project, the compiler can scaffold a configuration file for you: