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
At a glance
- Purpose
- Static types for JavaScript
- File extension
- .ts ยท .tsx
- Runs in
- Compiled to JavaScript
- Usually used with
- JavaScript and its ecosystem
Specifications & further reading
Related TypeScript documentation
tsconfig.json Basics
tsconfig.json configures how the TypeScript compiler behaves for a project - which files to include, what JavaScript version to target, and which type-checking rules to enforce. It is the first file the compiler looks for when run without explicit file arguments.Declaration Files (.d.ts)
Declaration files contain only type information, no actual implementation - they describe the shape of existing JavaScript code so TypeScript can type-check against it. This is how you get autocomplete and type safety for plain JavaScript libraries, and how your own TypeScript library can ship types alongside its compiled JavaScript output.Interfaces
Interfaces in TypeScript define the structure of objects by specifying property names and their types. They act as contracts that ensure objects conform to specific shapes, providing type safety and better IDE support. Interfaces can be extended, merged, and used to type-check function parameters, return values, and object literals.Basic Types
TypeScript extends JavaScript with static types, letting the compiler catch type errors before code ever runs. Beyond the familiar string, number, and boolean, TypeScript adds any (disables checking entirely), unknown (a safer any that requires narrowing), void (a function returning nothing), and never (a value that can never occur, like a function that always throws).
tsconfig.json configures how the TypeScript compiler behaves for a project - which files to include, what JavaScript version to target, and which type-checking rules to enforce. It is the first file the compiler looks for when run without explicit file arguments.Declaration Files (.d.ts)
Declaration files contain only type information, no actual implementation - they describe the shape of existing JavaScript code so TypeScript can type-check against it. This is how you get autocomplete and type safety for plain JavaScript libraries, and how your own TypeScript library can ship types alongside its compiled JavaScript output.Interfaces
Interfaces in TypeScript define the structure of objects by specifying property names and their types. They act as contracts that ensure objects conform to specific shapes, providing type safety and better IDE support. Interfaces can be extended, merged, and used to type-check function parameters, return values, and object literals.Basic Types
TypeScript extends JavaScript with static types, letting the compiler catch type errors before code ever runs. Beyond the familiar string, number, and boolean, TypeScript adds any (disables checking entirely), unknown (a safer any that requires narrowing), void (a function returning nothing), and never (a value that can never occur, like a function that always throws).