tsconfig.json Basics
In this page:
Creating tsconfig.json
A tsconfig.json file is a JSON configuration file placed at the root of a TypeScript project. It tells the TypeScript compiler which files belong to the project and exactly how they should be checked and compiled, so every developer and tool uses the same settings.
Example: Creating tsconfig.json
// tsconfig.json
// {
// "compilerOptions": { "target": "ES2020" }
// }
console.log("tsconfig.json configures the whole TypeScript project");
Compiler Options
The compilerOptions property is where most configuration lives, containing settings that control both type checking strictness and the shape of the generated JavaScript. Common options include target, module, strict, rootDir, and outDir.
Example: Compiler Options
// tsconfig.json
// {
// "compilerOptions": { "target": "ES2020", "module": "commonjs", "strict": true }
// }
console.log("compilerOptions controls type checking and JS output shape");
Target and Module
The target option controls which JavaScript language version the compiler emits, such as ES2015 or ES2022, affecting which modern syntax gets left alone versus transformed for older environments. The module option separately controls the module system, like CommonJS or ESNext, used in the generated output.
Example: Target and Module
// tsconfig.json
// {
// "compilerOptions": { "target": "ES2015", "module": "commonjs" }
// }
console.log("target picks the JS version; module picks the module format");
Strict Type Checking
The strict option is a single switch that enables a broad set of stronger type-checking rules at once, including strict null checks and stricter function types. Turning it on catches far more potential mistakes and is standard practice for new TypeScript projects.
Example: Strict Type Checking
// tsconfig.json
// { "compilerOptions": { "strict": true } }
let value: number | null = null;
// value.toFixed(2); // strict null checks reject this without a null check
console.log(value);
Including and Excluding Files
The include and exclude settings control which files the compiler actually treats as part of the project, using glob patterns. This matters when a project directory contains generated output, test files, or node_modules that should never be type-checked or recompiled.
Example: Including and Excluding Files
// tsconfig.json
// {
// "include": ["src/**/*"],
// "exclude": ["node_modules", "dist"]
// }
console.log("include/exclude use glob patterns to select project files");
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: