Lesson 18 of 20

TypeScript Configuration

Key Compiler Options

The tsconfig.json file controls how TypeScript compiles your code. Understanding key options helps you configure projects correctly.

Example
{
  "compilerOptions": {
    // Target JavaScript version
    "target": "ES2020",
    // Module system
    "module": "ESNext",
    // Enable strict type checking
    "strict": true,
    // Output directory
    "outDir": "./dist",
    // Source directory
    "rootDir": "./src",
    // Generate source maps for debugging
    "sourceMap": true,
    // Allow importing JSON files
    "resolveJsonModule": true,
    // Ensure consistent casing in imports
    "forceConsistentCasingInFileNames": true
  }
}

Strict Mode Options

The strict flag enables a set of strict type-checking options. You can also enable them individually.

Example
{
  "compilerOptions": {
    // Enable all strict checks at once
    "strict": true,

    // Or enable individually:
    "strictNullChecks": true,        // null/undefined not assignable
    "strictFunctionTypes": true,     // strict function parameter checking
    "strictBindCallApply": true,     // strict bind/call/apply
    "strictPropertyInitialization": true, // class properties must be initialized
    "noImplicitAny": true,           // error on implicit 'any' types
    "noImplicitThis": true,          // error on implicit 'this'
    "alwaysStrict": true             // emit 'use strict' in JS
  }
}

Project References

For large codebases, project references let you split your code into smaller projects that reference each other.

Example
// tsconfig.json (root)
{
  "references": [
    { "path": "./packages/shared" },
    { "path": "./packages/server" },
    { "path": "./packages/client" }
  ]
}

// packages/shared/tsconfig.json
{
  "compilerOptions": {
    "composite": true,
    "outDir": "./dist"
  }
}

// Build all projects:
// tsc --build