Code Reference

Strict compiler options

TypeScript · Reference cheat sheet

Strict compiler options

TypeScript · Reference cheat sheet


📋 Overview

strict: true enables a suite of soundness checks. Turn it on for new projects; migrate incrementally with per-flag toggles. TS 5.x also adds related flags (exactOptionalPropertyTypes, noUncheckedIndexedAccess, verbatimModuleSyntax).

🔧 Core concepts

Flag (under strict)Effect
noImplicitAnyError on implied any
strictNullChecksnull / undefined not in every type
strictFunctionTypesContravariant params for function types
strictBindCallApplyTyped bind / call / apply
strictPropertyInitializationClass props must be set
noImplicitThisthis must be typed
alwaysStrictEmit "use strict"
useUnknownInCatchVariablescatch (e)unknown

Extra (recommended):

  • noUncheckedIndexedAccessT[K] includes undefined.
  • exactOptionalPropertyTypes — distinguish missing vs undefined.
  • noImplicitReturns / noFallthroughCasesInSwitch.
  • verbatimModuleSyntax — type-only imports must use import type.

💡 Examples

// tsconfig.json
{
  "compilerOptions": {
    "strict": true,
    "noUncheckedIndexedAccess": true,
    "exactOptionalPropertyTypes": true,
    "noImplicitOverride": true,
    "useUnknownInCatchVariables": true,
    "verbatimModuleSyntax": true
  }
}
// strictNullChecks
function len(s: string | null) {
  // return s.length; // error
  return s?.length ?? 0;
}

// strictPropertyInitialization
class User {
  name!: string; // definite assignment assertion — use sparingly
  constructor(name: string) {
    this.name = name;
  }
}

try {
  /* ... */
} catch (e) {
  if (e instanceof Error) console.error(e.message);
}

import type { UserId } from "./ids"; // verbatimModuleSyntax

⚠️ Pitfalls

  • Enabling strict on a large JS codebase surfaces many errors — fix by folder / flag.
  • noUncheckedIndexedAccess adds | undefined everywhere — handle explicitly.
  • exactOptionalPropertyTypes breaks \{ prop?: T \} assignability with undefined.
  • skipLibCheck: true hides issues in .d.ts (common, but masks problems).
  • Don’t disable strict to “save time” — prefer local as / narrowing.

On this page