π tsconfig.json is a config file that tells the TypeScript compiler (tsc):
Which files to compile
How to compile them (rules, target JS version, strictness, etc.)
When you run tsc (without specifying a file), it reads tsconfig.json to guide compilation.
π How to create one
tsc --initπ This generates a default tsconfig.json.
βοΈ Basic structure
{
"compilerOptions": {
// Compiler settings go here
},
"include": [
// Files or folders to compile
],
"exclude": [
// Files or folders to ignore
]
}π Key compilerOptions to know
Option | What it does | Example |
|---|---|---|
target | JS version to output | "es6" |
module | Module system to use | "commonjs", "esnext" |
outDir | Where to put compiled JS | "./dist" |
rootDir | Where source TS files are | "./src" |
strict | Enables all strict type checks | true |
esModuleInterop | Allow default imports from CommonJS modules | true |
sourceMap | Generate | true |
declaration | Output | true |
noEmit | Donβt output JS, just check types | true |
removeComments | Remove comments from JS output | true |
allowJs | Allow JS files in the compilation | true |
checkJs | Type-check JS files | true |
lib | Include library files (e.g., DOM, ES2020) | [ "es2020", "dom" ] |
π Example tsconfig.json
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"sourceMap": true,
"declaration": false
},
"include": ["src/**/*"],
"exclude": ["node_modules", "**/*.spec.ts"]
}π This config:
Compiles TypeScript from
src/Outputs JS to
dist/Uses ES6 syntax + CommonJS modules
Enables strict type checking
Generates source maps for debugging
Ignores test files and
node_modules
β‘ Commonly used strict options
If you want to customize strictness:
{
"strictNullChecks": true,
"noImplicitAny": true,
"strictFunctionTypes": true,
"strictBindCallApply": true,
"alwaysStrict": true
}β Tip: Setting "strict": true automatically enables these.
π₯ Special options
Option | Use case |
|---|---|
incremental | Speeds up recompilation in large projects |
composite | Required for projects using project references |
noEmitOnError | Prevent JS output if type errors |
resolveJsonModule | Allow importing |
isolatedModules | Ensures compatibility with Babel or other transpilers |
π How to use
π Once tsconfig.json is set:
tscβ TypeScript will read this config and compile accordingly.
π‘ Summary
β
tsconfig.json gives you full control over TypeScript compilation.
β Helps make builds consistent across teams.
β Can be customized for Node, React, libraries, etc.