Generating tsconfig.json: What's Under the Hood?

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why tsconfig.json is More Than Just a File?



Everyone who has ever written TypeScript has encountered the mysterious tsconfig.json file. If you're a beginner, you've probably just copied it from the documentation or Googled "how to generate tsconfig.json". And if you're an experienced developer, you might have once wondered: what actually happens under the hood when I run tsc --init?



In this article, we won't just give you a ready-made recipe; we'll look into the engine room of the TypeScript compiler. You'll learn how the compiler reads configuration, which flags are responsible for code strictness, and how to write your own minimal tsconfig.json "generator" on the fly. Let's go!



1. What is tsconfig.json and Why Do You Need It?



tsconfig.json is a JSON file that tells the TypeScript compiler (tsc) exactly how to process your code. Without it, tsc will work in "default" mode, but this is often not enough for serious projects.



Under the hood, tsconfig.json defines:

  • Which files to compile (the include, exclude, files keys).
  • Where to put the output (the outDir key).
  • Which JavaScript versions to support (the target key).
  • How strictly to check types (the strict, noImplicitAny keys, etc.).
  • Additional modules and paths.


When you ask "how can I generate a tsconfig.json file", the answer is usually one: tsc --init. But let's figure out what exactly this command generates.



2. The tsc --init Command: What's Under the Hood?



Run this in your terminal:

npx tsc --init


A file called tsconfig.json will appear in the project root with a bunch of commented-out settings. In fact, under the hood, the TypeScript compiler executes the following algorithm:



  1. Checks if tsconfig.json already exists in the current directory.
  2. If not, creates a template with default settings.
  3. Sets target: "es2016", module: "commonjs", strict: true, and other sensible values.
  4. Comments out all other options so the developer can uncomment them as needed.


But what if you want to customize the generation? For example, to immediately enable strict mode or set a path for output files? You can write your own script for that.



2.1. Example: Your Own tsconfig.json Generator in TypeScript



Imagine you're writing a tool for automatically configuring projects. Here's what a function that generates tsconfig.json under the hood might look like:



import * as fs from 'fs';import * as path from 'path';

interface TsConfig { compilerOptions: { target: string; module: string; strict: boolean; outDir: string; rootDir: string; esModuleInterop: boolean; skipLibCheck: boolean; forceConsistentCasingInFileNames: boolean; }; include: string[]; exclude: string[];}

function generateTsConfig( target: string = 'ES2020', outDir: string = './dist', rootDir: string = './src', strict: boolean = true): TsConfig { return { compilerOptions: { target, module: 'commonjs', strict, outDir, rootDir, esModuleInterop: true, skipLibCheck: true, forceConsistentCasingInFileNames: true, }, include: ['src/**/*'], exclude: ['node_modules', 'dist'], };}

// Generate and saveconst config = generateTsConfig();fs.writeFileSync( path.join(process.cwd(), 'tsconfig.json'), JSON.stringify(config, null, 2));console.log('tsconfig.json created!');

Blogs

Book Recommendations