TypeScript Config & Strict Mode
The tsconfig.json file controls how TypeScript compiles and type-checks your project.
Basic Structure
{
"compilerOptions": {
"target": "ES2020",
"module": "ESNext",
"strict": true,
"outDir": "./dist",
"rootDir": "./src"
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}
Remember: Without tsconfig.json, TypeScript uses default options that may not suit your project.
Strict Mode
strict enables all strict type-checking options:
strictNullChecks— Prevents assigning null/undefined to non-nullable typesnoImplicitAny— Flags implicitanytypesstrictFunctionTypes— Strict function parameter checkingstrictPropertyInitialization— Ensures class properties are initialized
Always enable strict mode — it catches the most common TypeScript bugs.
Essential Options
{
"compilerOptions": {
"target": "ES2020",
"strict": true,
"noEmit": true,
"declaration": true,
"sourceMap": true,
"moduleResolution": "node",
"esModuleInterop": true,
"resolveJsonModule": true
}
}
noEmit— Type check only, no output (great for CI/CD)declaration— Generate.d.tsfiles for librariessourceMap— Enable debugging
Including and Excluding Files
{
"include": ["src/**/*"],
"exclude": ["node_modules", "dist", "**/*.test.ts"],
"files": ["src/main.ts"]
}
Tip: include uses glob patterns; files lists specific files.
Project References
Split large projects into smaller, manageable units:
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"outDir": "./dist",
"rootDir": "./src"
},
"references": [{ "path": "./tsconfig.lib.json" }]
}
Remember: Project references enable faster builds and better separation of concerns.
Best Practices
- Always enable strict mode
- Use source maps in development for debugging
- Generate declarations for libraries with
declaration: true - Use project references for large codebases
- Enable
noUnusedLocalsandnoUnusedParametersto clean up dead code
Common Mistakes
- Not enabling strict mode misses many type errors
- Using a target that’s too old forces unnecessary transpilation
- Not setting
rootDircan cause confusing output paths - Not excluding test files can cause type conflicts