A photo of Maximilian Schwarzmüller

Maximilian Schwarzmüller

Making Sense of tsconfig

Making Sense of tsconfig

Published

The tsconfig.json file is a crucial file in every TypeScript project, because this file configures which code files get checked and compiled by TypeScript, how those files get validated and what the output (if any) of the compilation process should be.

Unfortunately, when using tsc --init, a newly generated tsconfig.json file is often not configured appropriately for modern web development.

In addition, understanding all the various settings and their possible values (and effects) can be a nightmare - though the official documentation page generally does a good job at explaining most options. The problem just is, that (for historic reasons) there are many settings you’ll rarely (or never) need.

Fortunately, you often don’t need to set up the tsconfig.json file, though. Typically, you’ll create new projects (e.g., new React, Expo, Angular projects) with help of some build tool or templating tool (e.g., Vite ). When choosing TypeScript as a language, those tools generate well-defined tsconfig.json files for you.

A Solid Base tsconfig.json File

If you’re working in a project that doesn’t come with a pre-configured tsconfig.json file you can consider using the following configuration as a good base:

{
  "compilerOptions": {
    "esModuleInterop": true, // Ensures ESM and CJS imports work together well
    "skipLibCheck": true, // Ensures .d.ts files from 3rd libraries are not type-checked
    "target": "ES2022", // Sets a relatively modern ECMAScript version as compilation target
    "allowJs": true, // Allows importing .js files into .ts (helpful when migrating projects)
    "strict": true, // Ensures strict type checking (i.e., noImplicitAny etc)
    "noUncheckedIndexedAccess": true, // Adds undefined as a value when accessing by index
    // "noImplicitOverride": true, // Enable this when working with classes & inheritance
    "noUnusedLocals": true, // to avoid unused variables
    "module": "NodeNext", // Supports both ESM & CJS modules / imports; ESNext for frontend projects
    "outDir": "dist", // Store compiled files in "dist" folder
    "sourceMap": true, // Enables source maps for easier debugging
    "lib": ["ES2022", "DOM", "DOM.Iterable"] // Or without "dom" libs when building for Node
  }
}

Below you find a short explanation of these core settings.

Core Settings