# Strict Mode Required in tsconfig.json
## Rule
Every TypeScript project MUST set `"strict": true` in tsconfig.json. Additionally, enable noUncheckedIndexedAccess, exactOptionalPropertyTypes, and noImplicitOverride.
## Format
```json
{
"compilerOptions": {
"strict": true,
"noUncheckedIndexedAccess": true,
"exactOptionalPropertyTypes": true,
"noImplicitOverride": true,
"noFallthroughCasesInSwitch": true,
"forceConsistentCasingInFileNames": true,
"noPropertyAccessFromIndexSignature": true
}
}
```
## What Strict Enables
| Flag | Effect |
|------|--------|
| strictNullChecks | null/undefined require explicit handling |
| strictFunctionTypes | Correct function type variance |
| strictBindCallApply | Type-check bind/call/apply |
| strictPropertyInitialization | Class properties must be initialized |
| noImplicitAny | No implicit 'any' type |
| noImplicitThis | 'this' must have explicit type |
| alwaysStrict | Emit "use strict" in every file |
## Good Examples
```typescript
// strict mode catches these at compile time
function getUser(id: string): User | undefined {
return users.get(id);
}
const user = getUser("123");
// user is User | undefined — must check before use
if (user) {
console.log(user.name); // Safe — narrowed to User
}
// noUncheckedIndexedAccess
const items: string[] = ["a", "b", "c"];
const first = items[0]; // Type: string | undefined
if (first !== undefined) {
console.log(first.toUpperCase()); // Safe
}
// Index signatures
interface Config {
[key: string]: string;
}
const config: Config = { host: "localhost" };
const value = config["port"]; // Type: string | undefined (with noUncheckedIndexedAccess)
```
## Bad Examples
```json
{
"compilerOptions": {
"strict": false
}
}
```
```typescript
// Without strict mode, these bugs compile silently:
function greet(name) { // Implicit any parameter
return name.toUpperCase(); // Crashes if name is undefined
}
const arr = [1, 2, 3];
const val = arr[10]; // Type: number (wrong! it's undefined)
val.toFixed(2); // Runtime crash
```
## Enforcement
- CI: `tsc --noEmit` with strict tsconfig
- Never use `// @ts-ignore` — use `// @ts-expect-error` with comment
- ESLint: @typescript-eslint/no-explicit-any