What is Strict Mode in tsconfig.json?
Understand what "strict": true does in tsconfig.json, which flags it enables like strictNullChecks and noImplicitAny, and how to adopt it gradually.
Expected Interview Answer
Strict mode is a tsconfig.json setting, enabled with "strict": true, that turns on a bundle of stricter type-checking flags at once to catch more bugs at compile time.
Setting strict to true enables a family of options together, including strictNullChecks, noImplicitAny, strictFunctionTypes, strictBindCallApply, strictPropertyInitialization, alwaysStrict, and useUnknownInCatchVariables. Each flag can also be toggled individually, so teams can adopt strictness incrementally. The result is that null and undefined must be handled explicitly, implicit any is forbidden, and function types are checked more soundly, dramatically reducing runtime errors.
- Catches null and undefined errors at compile time
- Forbids accidental implicit any types
- Enforces class property initialization
- Enables sounder function type checking
- Improves refactoring safety and code reliability
AI Mentor Explanation
Strict mode is like a fussy third umpire who reviews every marginal decision instead of waving play on. Nothing gets the benefit of the doubt: no-balls, faint edges, and close run-outs are all checked before the game continues. Turning strict off is trusting the on-field call for everything, which is faster but lets wrong decisions slip through and cost you later.
Step-by-Step Explanation
Step 1
Open tsconfig.json
Locate the compilerOptions object where TypeScript's behavior is configured.
Step 2
Set strict to true
Add "strict": true inside compilerOptions to enable the whole strict family at once.
Step 3
Understand the flags enabled
This turns on strictNullChecks, noImplicitAny, strictFunctionTypes, strictPropertyInitialization, and more together.
Step 4
Fix surfaced errors
Handle newly reported nulls, add explicit types, and initialize class properties the compiler now flags.
Step 5
Toggle individually if migrating
For large legacy codebases, enable single flags like strictNullChecks incrementally instead of all at once.
What Interviewer Expects
- Knowing strict is an umbrella flag, not a single check
- Naming at least a few flags it enables, especially strictNullChecks and noImplicitAny
- Understanding that flags can be toggled individually for gradual adoption
- Explaining the practical bugs strict prevents
- Awareness of the migration cost on legacy code
Common Mistakes
- Thinking strict enables only strictNullChecks
- Believing strict mode affects runtime behavior rather than compile-time checks
- Enabling strict on a huge legacy codebase all at once and getting overwhelmed
- Confusing TypeScript strict with JavaScript's 'use strict' directive
- Assuming individual strict flags cannot be turned off once strict is true
Best Answer (HR Friendly)
“Strict mode is a single setting in the TypeScript config file that switches on several tougher checks together. It makes the compiler catch common mistakes, like forgetting a value could be null, before the code ever runs, so the software is more reliable.”
Code Example
// tsconfig.json
{
"compilerOptions": {
"strict": true
}
}
// With strict on, these are compile errors:
function greet(name: string) {
return name.toUpperCase()
}
const maybe: string | null = null
// Error: 'maybe' is possibly 'null'
greet(maybe)
// noImplicitAny catches this:
function add(a, b) { // Error: parameters implicitly have 'any' type
return a + b
}Follow-up Questions
- Which individual flags does "strict": true enable?
- How does strictNullChecks change how you handle optional values?
- How would you migrate a large JavaScript codebase to strict mode gradually?
- What is the difference between noImplicitAny and strict?
- What does strictPropertyInitialization require in classes?
MCQ Practice
1. What does setting "strict": true do?
strict is an umbrella flag that turns on several stricter checks together, including strictNullChecks and noImplicitAny.
2. Which behavior does strictNullChecks enforce?
strictNullChecks removes null and undefined from every type by default, so they must be handled explicitly.
3. After enabling strict, can you disable an individual flag?
You can set an individual flag like "strictNullChecks": false after "strict": true to opt out of just that check.
Flash Cards
What is strict mode in tsconfig? — A setting ("strict": true) that enables a bundle of stricter type-checking flags together.
Name three flags strict enables — strictNullChecks, noImplicitAny, and strictFunctionTypes (among others).
Does strict affect runtime? — No — it only changes compile-time type checking, not runtime behavior.
Can you adopt strict gradually? — Yes — enable individual flags incrementally instead of all at once for legacy code.