TypeScript Compiler Options

The compilerOptions section of a tsconfig.json file is crucial for configuring how the TypeScript compiler processes your code. It allows you to control various aspects of the compilation process, such as module resolution, target output, and strict type-checking. These options help you tailor the behavior of the TypeScript compiler to fit your project's needs.

 

Key Compiler Options

target Specifies the JavaScript version that TypeScript should compile to.

  • Values: ES3, ES5, ES6/ES2015, ES2016, ES2017, ES2018, ES2019, ES2020, ESNext
  • Default: ES3

module Defines the module system to be used in the output JavaScript.

  • Values: CommonJS, AMD, UMD, System, ES6, ESNext, None
  • Default: CommonJS

strict Enables all strict type-checking options, providing stricter and safer type checking in TypeScript.

  • Values: true or false
  • Default: false

esModuleInterop Allows default imports from modules that do not have a default export. It helps in interoperation between TypeScript and CommonJS modules.

  • Values: true or false
  • Default: false

moduleResolution Determines how the TypeScript compiler resolves module imports.

  • Values: Node, Classic
  • Default: Node

outDir Specifies the output directory where the compiled JavaScript files should be stored.

  • Example:

rootDir Specifies the root directory of the source files. This is helpful when organizing your source files into separate directories.

  • Example:

declaration Generates corresponding .d.ts declaration files alongside the JavaScript files. This is useful for sharing your TypeScript code with other projects.

  • Values: true or false
  • Default: false

sourceMap Generates corresponding .map files to map the compiled JavaScript code back to TypeScript, which is useful for debugging.

  • Values: true or false
  • Default: false

skipLibCheck Skips type-checking of declaration files (.d.ts), improving compile times.

  • Values: true or false
  • Default: false

allowJs Allows JavaScript files to be included in the compilation process alongside TypeScript files.

  • Values: true or false
  • Default: false

noImplicitAny Requires that TypeScript throws an error when it infers the any type for variables and parameters without explicit type annotations.

  • Values: true or false
  • Default: false

strictNullChecks Ensures that null and undefined are treated as distinct types, providing stricter null checking.

  • Values: true or false
  • Default: false

forceConsistentCasingInFileNames Ensures that file imports are case-sensitive on all systems, preventing potential issues on case-sensitive file systems.

  • Values: true or false
  • Default: false

noUnusedLocals Throws an error if there are unused local variables in the TypeScript files.

  • Values: true or false
  • Default: false

noUnusedParameters Throws an error if there are unused function parameters in the TypeScript files.

  • Values: true or false
  • Default: false

resolveJsonModule Allows importing JSON files as modules in TypeScript.

  • Values: true or false
  • Default: false

lib Specifies a list of library files to include in the compilation. This is useful for including built-in libraries like DOM, ES2015, etc.

  • Example:

Example of a tsconfig.json with Compiler Options

{
  "compilerOptions": {
    "target": "ES6",
    "module": "commonjs",
    "strict": true,
    "esModuleInterop": true,
    "outDir": "./dist",
    "declaration": true,
    "sourceMap": true,
    "skipLibCheck": true,
    "allowJs": false,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "forceConsistentCasingInFileNames": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true
  },
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ]
}

 

Summary

  • compilerOptions in the tsconfig.json file control how the TypeScript compiler behaves, allowing you to define settings such as the target JavaScript version, module system, and strict type checking.
  • Commonly used options include target, module, strict, esModuleInterop, and sourceMap.
  • tsconfig.json helps customize the compilation process, enabling better type safety, debugging support, and performance optimizations.