~/hackweb.dev
TypeScript Config & Strict Mode
Quiz
...

TypeScript Config & Strict Mode

beginner · updated Tue Sep 08 2026Contribute

Configure TypeScript projects with tsconfig.json and strict mode.

TypeScript Config & Strict Mode

The tsconfig.json file controls how TypeScript compiles and type-checks your project.

Basic Structure

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "ESNext",
    "strict": true,
    "outDir": "./dist",
    "rootDir": "./src"
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules"]
}

Remember: Without tsconfig.json, TypeScript uses default options that may not suit your project.

Strict Mode

strict enables all strict type-checking options:

  • strictNullChecks — Prevents assigning null/undefined to non-nullable types
  • noImplicitAny — Flags implicit any types
  • strictFunctionTypes — Strict function parameter checking
  • strictPropertyInitialization — Ensures class properties are initialized

Always enable strict mode — it catches the most common TypeScript bugs.

Essential Options

{
  "compilerOptions": {
    "target": "ES2020",
    "strict": true,
    "noEmit": true,
    "declaration": true,
    "sourceMap": true,
    "moduleResolution": "node",
    "esModuleInterop": true,
    "resolveJsonModule": true
  }
}
  • noEmit — Type check only, no output (great for CI/CD)
  • declaration — Generate .d.ts files for libraries
  • sourceMap — Enable debugging

Including and Excluding Files

{
  "include": ["src/**/*"],
  "exclude": ["node_modules", "dist", "**/*.test.ts"],
  "files": ["src/main.ts"]
}

Tip: include uses glob patterns; files lists specific files.

Project References

Split large projects into smaller, manageable units:

{
  "extends": "./tsconfig.base.json",
  "compilerOptions": {
    "outDir": "./dist",
    "rootDir": "./src"
  },
  "references": [{ "path": "./tsconfig.lib.json" }]
}

Remember: Project references enable faster builds and better separation of concerns.

Best Practices

  • Always enable strict mode
  • Use source maps in development for debugging
  • Generate declarations for libraries with declaration: true
  • Use project references for large codebases
  • Enable noUnusedLocals and noUnusedParameters to clean up dead code

Common Mistakes

  • Not enabling strict mode misses many type errors
  • Using a target that’s too old forces unnecessary transpilation
  • Not setting rootDir can cause confusing output paths
  • Not excluding test files can cause type conflicts