How to configure tsconfig.json

· Category: TypeScript

Short answer

tsconfig.json is the configuration file for the TypeScript compiler. It specifies root files, compiler options, and project references.

Steps

  1. Generate a default file with npx tsc --init.
  2. Set compilerOptions.target to your runtime (e.g., ES2020).
  3. Set module to CommonJS or ESNext depending on your environment.
  4. Specify outDir for compiled output and rootDir for source files.
  5. Add include and exclude arrays to control which files are compiled.

Tips

  • Use strict: true to enable a comprehensive set of type-checking options.
  • esModuleInterop simplifies importing CommonJS modules from ES modules.

Common issues

  • Misaligned module and moduleResolution settings cause import resolution failures.
  • Including node_modules in compilation slows the build and produces irrelevant errors.