TypeScript Compiler Options

The TypeScript compiler (tsc) has numerous compiler options. This tutorial provides a detailed introduction to commonly used compiler options and their purposes.

SVG Diagram: TypeScript Compilation Process Background Title TypeScript Compilation Process and Compiler Options Source Code TypeScript Source Code .ts / .tsx Contains Type Annotations Arrow 1 TypeScript Compiler TypeScript Compiler (tsc) Compiler Options Compiler Option Functions: • Type checking (strict) • Output format (module) • Target version (target) • Declaration files (declaration) ...More options Arrow 2 Compilation Output Compilation Output .js - JavaScript .d.ts - Type Declarations .map - Source Map Lower Section: Compiler Option Categories Compiler Option Categories Category 1 Type Checking strict noImplicitAny strictNullChecks Category 2 Output Control outDir declaration sourceMap Category 3 Module System module moduleResolution esModuleInterop Category 4 Language Features target lib jsx Arrow Marker

Output Control Options

Example

{
    "compilerOptions": {
        // Output directory
        "outDir": "./dist",

        // Source root directory
        "rootDir": "./src",

        // Generate .d.ts declaration files
        "declaration": true,

        // Declaration file output directory
        "declarationDir": "./types",

        // Generate source maps
        "sourceMap": true,

        // Generate .js.map files
        "mapRoot": "./map"
    }
}

Type Checking Options

Example

{
    "compilerOptions": {
        // Strict mode (recommended to always enable)
        "strict": true,

        // Check null and undefined
        "strictNullChecks": true,

        // Check this parameter
        "noImplicitThis": true,

        // Strict function types
        "strictFunctionTypes": true,

        // Strict property initialization
        "strictPropertyInitialization": true,

        // Disallow implicit any
        "noImplicitAny": true,

        // Disallow returning void
        "noImplicitReturns": true,

        // Enable all strict checks
        "strict": true
    }
}

Module Options

Example

{
    "compilerOptions": {
        // Module system
        "module": "commonjs",

        // Module resolution strategy
        "moduleResolution": "node",

        // Base path for resolution
        "baseUrl": ".",

        // Path aliases
        "paths": {
            "@/*": ["src/*"]
        },

        // ES module interoperability
        "esModuleInterop": true,

        // Allow default imports
        "allowSyntheticDefaultImports": true,

        // Isolate modules
        "isolatedModules": true
    }
}

ES Feature Options

Example

{
    "compilerOptions": {
        // Compilation target
        "target": "ES2020",

        // Enabled libraries
        "lib": ["ES2020", "DOM"],

        // Allow unused local variables
        "noUnusedLocals": true,

        // Allow unused parameters
        "noUnusedParameters": true,

        // Code downleveling
        "downlevelIteration": true
    }
}

Experimental Options

Example

{
    "compilerOptions": {
        // Enable decorators
        "experimentalDecorators": true,

        // Enable decorator metadata
        "emitDecoratorMetadata": true,

        // Enable async iterators
        "emitDecoratorMetadata": true,

        // Skip library checks
        "skipLibCheck": true
    }
}

Common Compiler Option Combinations

Example

// Recommended configuration for Node.js projects
{
    "compilerOptions": {
        "target": "ES2020",
        "module": "commonjs",
        "lib": ["ES2020"],
        "outDir": "./dist",
        "rootDir": "./src",
        "strict": true,
        "esModuleInterop": true,
        "skipLibCheck": true,
        "forceConsistentCasingInFileNames": true,
        "moduleResolution": "node",
        "declaration": true
    }
}

Compiler Options Table

Category Common Options
Output outDir, rootDir, declaration, sourceMap
Type Checking strict, strictNullChecks, noImplicitAny
Module module, moduleResolution, paths, esModuleInterop
ES Features target, lib, downlevelIteration
Experimental experimentalDecorators, emitDecoratorMetadata

Summary

  • Strict mode:Always enable strict: true
  • Target version:Choose the appropriate target based on the environment
  • Module system:Choose module based on the deployment environment
  • Type declarations:Enable declaration when developing libraries
Other Extensions