TypeScript ยท Chapter 4 of 44

The tsconfig.json File

A tsconfig.json file marks the root of a TypeScript project and holds compiler options that control how your code is checked and compiled. You can create one automatically by running `tsc --init`.

With a tsconfig.json in place, you can simply run `tsc` with no arguments, and it will compile your whole project according to the settings you defined, such as which JavaScript version to target and how strict the type checking should be.

Syntax
{
  "compilerOptions": {
    "target": "ES2020",
    "outDir": "dist",
    "strict": true
  }
}

Common options

`target` sets which JavaScript version to output (like ES2020). `outDir` sets where compiled files go. `strict` turns on a group of strict type-checking rules, which is strongly recommended.

Generating a config

Run `tsc --init` in your project folder to generate a tsconfig.json with helpful comments explaining each option, which you can then customize.

Example 1 (bash)
tsc --init
Output
Created a new tsconfig.json

Generates a default configuration file in the current folder.

Example 2 (json)
{
  "compilerOptions": {
    "target": "ES2020",
    "module": "CommonJS",
    "strict": true,
    "outDir": "dist"
  },
  "include": ["src/**/*"]
}
Output
(no runtime output โ€” configuration file)

This config compiles everything in src into ES2020 JavaScript inside a dist folder with strict checks.

Key points

  • tsconfig.json defines the root and settings of a TypeScript project.
  • `tsc --init` generates a starter configuration file.
  • `strict: true` enables the most helpful type-checking rules.
  • `outDir` and `target` control where and how code is compiled.
๐Ÿ’ก Note: Always enable `strict` mode on new projects โ€” it catches far more bugs early.

๐Ÿ“ Quick Quiz

1. What command generates a tsconfig.json file?

2. What does the `strict` option do?

3. What does `outDir` control?