Skip to content

Strict Mode in TypeScript

Enable strict compiler flags to catch the most bugs: null checks, implicit any, and more.

What you'll learn

  • What strict: true enables and why it is recommended
  • Individual strict flags and their specific protections
  • How to migrate an existing project to strict mode incrementally
  • Additional strictness flags beyond the strict bundle

Concept

The Strict Bundle

Setting "strict": true in tsconfig.json enables a bundle of safety checks simultaneously. This includes strictNullChecks (null and undefined are not assignable to other types), noImplicitAny (parameters and variables must have explicit or inferable types), strictFunctionTypes (function parameter types are checked contravariantly), strictBindCallApply, strictPropertyInitialization (class properties must be initialized), noImplicitThis, and alwaysStrict (emits "use strict" in output).

Individual Flags

You can enable individual flags if you cannot adopt strict all at once. The most impactful is strictNullChecks — it alone prevents a huge class of runtime errors. noImplicitAny catches untyped parameters that would silently become any. strictPropertyInitialization ensures class properties are set in the constructor, catching uninitialized property bugs. Each flag can be toggled independently for gradual adoption.

Incremental Migration

Migrating an existing project to strict mode can produce many errors. The recommended approach is incremental: enable strictNullChecks first (it catches the most bugs), fix the errors, then enable noImplicitAny, and so on. You can also use // @ts-ignore or // @ts-expect-error to temporarily suppress specific errors while you work through them. The goal is full strict mode, but the path can be gradual.

Example

              // tsconfig.json — strict mode configuration.
{
  "compilerOptions": {
    // Option 1: Enable everything at once (recommended for new projects).
    "strict": true,

    // Option 2: Enable individual flags (for gradual migration).
    // "strictNullChecks": true,
    // "noImplicitAny": true,
    // "strictFunctionTypes": true,
    // "strictPropertyInitialization": true,
    // "noImplicitThis": true,
    // "alwaysStrict": true,

    // Additional strictness beyond the strict bundle.
    "noUnusedLocals": true,        // error on unused local variables
    "noUnusedParameters": true,    // error on unused parameters
    "noFallthroughCasesInSwitch": true, // error on switch fallthrough
    "noImplicitReturns": true,     // error on missing return paths
    "noUncheckedIndexedAccess": true    // arr[i] is T | undefined
  }
}

// With strict mode, the compiler catches these:
class User {
  name: string; // ❌ strictPropertyInitialization: not initialized
  constructor(public id: number) {}
}

function process(data: any) { // ❌ noImplicitAny: parameter is any
  return data.value;
}

// With noUncheckedIndexedAccess:
const arr: number[] = [1, 2, 3];
const item = arr[10]; // type: number | undefined
            

strict: true enables a bundle of safety checks. The most impactful are strictNullChecks and noImplicitAny. Migrate incrementally by enabling flags one at a time. Additional flags like noUnusedLocals add further strictness.

Try it

  • Code Diff

    Compare code that passes strict mode with code that fails it.

  • Code Minifier

    Strict mode affects emitted code — alwaysStrict adds 'use strict'.

Common mistakes

The mistake

Starting a new project without strict mode enabled

The fix

Always enable strict: true in new projects. Retrofitting strict mode later is much harder than starting with it. The flags catch bugs from day one.

The mistake

Using @ts-ignore to suppress strict mode errors instead of fixing them

The fix

@ts-ignore hides the error without fixing it. Fix the underlying type issue. If suppression is truly necessary, use @ts-expect-error so the compiler warns if the error disappears.

Practice Strict Mode in TypeScript

4 exercises

Practice Strict Mode in TypeScript

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors