Syntax
{ "compilerOptions": { "strict": true } }Examples
What strict: true Actually Enables
The individual flags bundled together by the strict shorthand.
{
"compilerOptions": {
"strict": true
// equivalent to enabling all of:
// "noImplicitAny": true,
// "strictNullChecks": true,
// "strictFunctionTypes": true,
// "strictBindCallApply": true,
// "strictPropertyInitialization": true,
// "noImplicitThis": true,
// "alwaysStrict": true
}
}strictNullChecks in Action
The single most impactful strict flag - catching a huge class of "cannot read property of undefined" bugs.
// Without strictNullChecks:
function getLength(str: string) {
return str.length; // no warning, even though str could be null/undefined
}
// With strictNullChecks:
function getLength(str: string | null) {
// return str.length; // Error - str could be null here
return str?.length ?? 0; // forces you to actually handle the null case
}Best practices
- Enable strict mode from the very start of a new project - retrofitting it onto a large existing codebase later is significantly more work
- If migrating an existing project, consider enabling individual strict flags one at a time (starting with strictNullChecks) rather than flipping strict all at once
- Treat strictNullChecks errors as genuine bugs to fix, not obstacles - they almost always represent a real, previously-unhandled null/undefined case
- Pair strict mode with noUncheckedIndexedAccess for even stronger guarantees around array/object index access returning possibly-undefined values
Runtime & versions
Compiled to JavaScript. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
TypeScript Handbook