Intermediate LevelJavaScript & TypeScript
What is TypeScript strict mode and what compiler flags are essential?
❓ Interview Prompt: Explain tsconfig strict: true flag and its sub-flags (strictNullChecks, noImplicitAny, strictPropertyInitialization).
Enabling "strict": true in tsconfig.json turns on a suite of strict type-checking compiler behaviors that maximize type safety.
Core Strict Compiler Flags:
1
`strictNullChecks`:
null and undefined are not assignable to other types unless explicitly unioned (string | null). Prevents "cannot read property of null" runtime bugs!2
`noImplicitAny`: Triggers an error whenever TypeScript cannot infer a type and defaults to
any.3
`strictPropertyInitialization`: Ensures class properties are initialized in the constructor or assigned default values.
4
`strictFunctionTypes`: Enforces strict bivariance checks on function parameters.
Code Implementation Example
typescript
// With strictNullChecks: true
let name: string = 'Alice';
// name = null; // ❌ Compile Error: Type 'null' is not assignable to type 'string'
let nullableName: string | null = null; // ✅ Explicit union allowed!
// With strictPropertyInitialization: true
class User {
username: string; // ❌ Compile Error: Property has no initializer!
email: string = ''; // ✅ Initialized
constructor(username: string) {
this.username = username; // ✅ Initialized in constructor
}
}💡strictNullChecks preventing unintended null assignment and strictPropertyInitialization rules.
🎯 Interview Key Takeaway
Always enable "strict": true in tsconfig.json to prevent runtime null pointer exceptions and implicit any fallbacks.
#TypeScript#tsconfig#Strict Mode#Intermediate
Back to all JavaScript & TypeScript Questions→