4 min read
•Question 33 of 48easyWhat is Strict Mode in TypeScript?
Enabling stricter type checking.
What You'll Learn
- What strict mode enables
- Individual strict flags
- Why use strict mode
Enabling Strict Mode
data.jsonJSON
// tsconfig.json
{
"compilerOptions": {
"strict": true
}
}What strict Enables
| Flag | Description |
|---|---|
| strictNullChecks | null/undefined are distinct types |
| strictFunctionTypes | Stricter function type checking |
| strictBindCallApply | Check bind, call, apply |
| strictPropertyInitialization | Class properties must be initialized |
| noImplicitAny | Error on implicit any |
| noImplicitThis | Error on implicit this |
| alwaysStrict | Emit "use strict" |
Examples
code.tsTypeScript
// strictNullChecks
let name: string = null; // Error!
// noImplicitAny
function fn(x) { } // Error: x has implicit any
// strictPropertyInitialization
class User {
name: string; // Error: not initialized
// Fix:
name: string = "";
// or
name!: string; // Definite assignment
}Why Use Strict?
- Catch more errors at compile time
- Better IDE support
- Safer refactoring
- Recommended for new projects