TypeScripttsconfig options
"strict"
Turns on a set of the most important, rigorous compiler checks.
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
#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
strictat once.
Related entries
#- "noImplicitAny"Reports an error when the compiler would have to silently assume the any type.
- "strictNullChecks"Treats null and undefined as separate types, forcing you to handle them.
- "target"The JavaScript version the compiler turns your code into.
- unknownThe safe counterpart to any: a value of an unknown type that you must check before use.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.