TypeScript Tsconfig Options

Configure tsconfig compiler options for strict type safety, modular module resolution, paths, and build outputs.

TL;DR

  1. Generate starter configuration files quickly using tsc --init in projects.
  2. Enable strict mode to catch the most critical type errors.
  3. Configure include and exclude arrays to control compiled source files.

Project Initialization

    Generate starter config

    Create standard starter configuration.

    // Run from terminal:
    // tsc --init
    Compiler options block

    Root object containing compiler flags.

    {
      "compilerOptions": {
        "target": "ES2022",
        "module": "NodeNext"
      }
    }
    Type check without emit

    Validate types without generating files.

    // Run in terminal or CI:
    // tsc --noEmit
    Extend shared configs

    Inherit options from a base config.

    {
      "extends": "@tsconfig/node20/tsconfig.json",
      "compilerOptions": {
        "outDir": "./dist"
      }
    }

Strict Type Checking

    Master strict flag

    Turn on all strict type checking flags.

    {
      "compilerOptions": {
        "strict": true
      }
    }
    Strict null checks

    Disallow implicit null or undefined.

    {
      "compilerOptions": {
        "strictNullChecks": true
      }
    }
    No implicit any

    Require explicit types for inferred any.

    {
      "compilerOptions": {
        "noImplicitAny": true
      }
    }
    Strict property init

    Ensure class properties are initialized.

    {
      "compilerOptions": {
        "strictPropertyInitialization": true
      }
    }

Emit and Target Control

    ECMAScript target

    Specify target JavaScript output version.

    {
      "compilerOptions": {
        "target": "ES2022"
      }
    }
    Module code generation

    Specify module system for emit.

    {
      "compilerOptions": {
        "module": "NodeNext",
        "moduleResolution": "NodeNext"
      }
    }
    Output directory

    Redirect emitted JavaScript files.

    {
      "compilerOptions": {
        "rootDir": "./src",
        "outDir": "./dist"
      }
    }
    Declaration emit

    Generate type definition files.

    {
      "compilerOptions": {
        "declaration": true,
        "declarationMap": true
      }
    }

File Inclusion and Paths

    Include source globs

    Specify source file match patterns.

    {
      "include": ["src/**/*", "tests/**/*"]
    }
    Exclude build folders

    Ignore dependency and output folders.

    {
      "exclude": ["node_modules", "dist"]
    }
    Path mapping aliases

    Map import specifiers to directories.

    {
      "compilerOptions": {
        "baseUrl": ".",
        "paths": {
          "@/*": ["src/*"]
        }
      }
    }
    Root directory guard

    Enforce single directory root structure.

    {
      "compilerOptions": {
        "rootDir": "./src"
      }
    }

Interop and Performance

    ES module interop

    Enable default imports from CommonJS.

    {
      "compilerOptions": {
        "esModuleInterop": true
      }
    }
    Skip library check

    Skip type checking in declaration files.

    {
      "compilerOptions": {
        "skipLibCheck": true
      }
    }
    Resolve JSON modules

    Permit importing JSON file data.

    {
      "compilerOptions": {
        "resolveJsonModule": true
      }
    }
    Incremental builds

    Cache build state to speed recompilation.

    {
      "compilerOptions": {
        "incremental": true,
        "tsBuildInfoFile": "./.tsbuildinfo"
      }
    }

Tips

  1. Enable skipLibCheck in projects to prevent redundant type-checking across large third-party declaration files and boost compilation performance.
  2. Set noEmitOnError to true during local builds to prevent JavaScript output generation whenever compilation encounters existing type diagnostics.

Warnings

  1. Configuring paths aliases in tsconfig.json only satisfies the compiler; you must also configure bundler resolve plugins for runtime execution.
  2. Changing target or module mid-project can trigger subtle runtime polyfill incompatibilities across current browsers and legacy Node runtimes.

In Practice

FAQ