"strict"

Turns on a set of the most important, rigorous compiler checks.

On this page

Example

#
JSON
{
  "compilerOptions": {
    "strict": true,
    "target": "ES2022",
    "module": "ESNext",
    "moduleResolution": "Bundler"
  }
}

Definition and usage

#

The strict: true option in tsconfig.json enables a whole group of checks at once, including noImplicitAny, strictNullChecks, strictFunctionTypes, strictBindCallApply, strictPropertyInitialization, and useUnknownInCatchVariables.

This is the recommended setting for every new project. It catches the most common mistakes: untyped parameters, reading from a value that may be null, and class fields with no assigned value.

New checks added to the strict group in later TypeScript versions turn on automatically. You can switch off an individual check from the group separately, for example during a gradual migration of a large project.

Syntax

#
Syntax
{
  "compilerOptions": {
    "strict": true
  }
}

More examples

#
What strict mode catches
TypeScript
function greet(name) {
  // Error (noImplicitAny): Parameter 'name' implicitly has an 'any' type.
  return 'Hello ' + name
}

const input = document.querySelector('input')
input.value = 'Ania'
// Error (strictNullChecks): 'input' is possibly 'null'.

Good practices

#
  • Start every new project with strict: true. Turning it on later in a large codebase is much harder.
  • When migrating from JavaScript, you can enable the checks in the group one by one instead of the whole strict at once.

Related entries

#

Practise it in lessons

#

Spotted a mistake or missing an example? Write to us.