TypeScript — Configuration
tsconfig.json
The TypeScript configuration file controls compiler behavior:
{
"compilerOptions": {
"target": "ES2020",
"module": "ESNext",
"strict": true,
"outDir": "./dist",
"rootDir": "./src"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}
Essential compiler options
target
What JavaScript version to compile to:
{
"compilerOptions": {
"target": "ES2020" // ES5, ES6, ES2015-ES2024, ESNext
}
}
module
Module system for output:
{
"compilerOptions": {
"module": "ESNext" // CommonJS, AMD, UMD, ESNext, NodeNext
}
}
strict
Enable all strict type-checking options:
{
"compilerOptions": {
"strict": true
}
}
This enables:
strictNullChecksstrictFunctionTypesstrictBindCallApplynoImplicitAnynoImplicitThisalwaysStrict
outDir and rootDir
{
"compilerOptions": {
"outDir": "./dist",
"rootDir": "./src"
}
}
Strict mode options
{
"compilerOptions": {
"strictNullChecks": true, // null/undefined checks
"strictFunctionTypes": true, // Function parameter checks
"strictBindCallApply": true, // Bind/call/apply checks
"noImplicitAny": true, // Error on implicit any
"noImplicitThis": true, // Error on implicit this
"alwaysStrict": true, // Emit "use strict"
"noImplicitReturns": true, // Error on missing returns
"noFallthroughCasesInSwitch": true // Error on switch fallthrough
}
}
Path mapping
{
"compilerOptions": {
"baseUrl": "./src",
"paths": {
"@utils/*": ["utils/*"],
"@types/*": ["types/*"],
"@components/*": ["components/*"]
}
}
}
// Now you can use clean imports
import { formatDate } from "@utils/date";
import { User } from "@types/models";
Declaration files
{
"compilerOptions": {
"declaration": true, // Generate .d.ts files
"declarationMap": true, // Generate declaration maps
"sourceMap": true // Generate source maps
}
}
JSX support
{
"compilerOptions": {
"jsx": "react-jsx" // react, react-jsx, preserve
}
}
Include and exclude
{
"include": ["src/**/*", "types/**/*"],
"exclude": ["node_modules", "dist", "**/*.test.ts"]
}
References
For project references (monorepos):
{
"references": [
{ "path": "./packages/shared" },
{ "path": "./packages/api" }
],
"files": []
}
Practical configs
Node.js project
{
"compilerOptions": {
"target": "ES2020",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"declaration": true,
"declarationMap": true,
"sourceMap": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}
React project
{
"compilerOptions": {
"target": "ES2020",
"module": "ESNext",
"moduleResolution": "bundler",
"jsx": "react-jsx",
"outDir": "./dist",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"isolatedModules": true
},
"include": ["src/**/*"]
}
Best practices
- Always enable
strict: true - Use
moduleResolution: "NodeNext"for modern projects - Enable
noUnusedLocalsandnoUnusedParametersfor cleanliness - Use
skipLibCheck: truefor faster compilation - Use
forceConsistentCasingInFileNames: truefor cross-platform
Mini Practice
- Create a
tsconfig.jsonwith strict mode enabled - Set up path aliases for your project
- Configure for a Node.js project
- Add declaration file generation
Up Next
Congratulations! You've completed the TypeScript fundamentals. Continue exploring advanced topics like decorators, template literal types, and conditional types.
Related Topics
Frequently Asked Questions about Configuration
What is Configuration in TypeScript?
Configuration is a fundamental concept in TypeScript. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Configuration?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Configuration.
Why is Configuration important in TypeScript?
Configuration is essential for TypeScript development. Understanding this concept will help you write better code and solve real-world problems more effectively.