TypeScript Compiler Options
The TypeScript compiler (tsc) has numerous compiler options. This tutorial provides a detailed introduction to commonly used compiler options and their purposes.
SVG Diagram: TypeScript Compilation ProcessOutput Control Options
Example
{
"compilerOptions": {
// Output directory
"outDir": "./dist",
// Source root directory
"rootDir": "./src",
// Generate .d.ts declaration files
"declaration": true,
// Declaration file output directory
"declarationDir": "./types",
// Generate source maps
"sourceMap": true,
// Generate .js.map files
"mapRoot": "./map"
}
}
"compilerOptions": {
// Output directory
"outDir": "./dist",
// Source root directory
"rootDir": "./src",
// Generate .d.ts declaration files
"declaration": true,
// Declaration file output directory
"declarationDir": "./types",
// Generate source maps
"sourceMap": true,
// Generate .js.map files
"mapRoot": "./map"
}
}
Type Checking Options
Example
{
"compilerOptions": {
// Strict mode (recommended to always enable)
"strict": true,
// Check null and undefined
"strictNullChecks": true,
// Check this parameter
"noImplicitThis": true,
// Strict function types
"strictFunctionTypes": true,
// Strict property initialization
"strictPropertyInitialization": true,
// Disallow implicit any
"noImplicitAny": true,
// Disallow returning void
"noImplicitReturns": true,
// Enable all strict checks
"strict": true
}
}
"compilerOptions": {
// Strict mode (recommended to always enable)
"strict": true,
// Check null and undefined
"strictNullChecks": true,
// Check this parameter
"noImplicitThis": true,
// Strict function types
"strictFunctionTypes": true,
// Strict property initialization
"strictPropertyInitialization": true,
// Disallow implicit any
"noImplicitAny": true,
// Disallow returning void
"noImplicitReturns": true,
// Enable all strict checks
"strict": true
}
}
Module Options
Example
{
"compilerOptions": {
// Module system
"module": "commonjs",
// Module resolution strategy
"moduleResolution": "node",
// Base path for resolution
"baseUrl": ".",
// Path aliases
"paths": {
"@/*": ["src/*"]
},
// ES module interoperability
"esModuleInterop": true,
// Allow default imports
"allowSyntheticDefaultImports": true,
// Isolate modules
"isolatedModules": true
}
}
"compilerOptions": {
// Module system
"module": "commonjs",
// Module resolution strategy
"moduleResolution": "node",
// Base path for resolution
"baseUrl": ".",
// Path aliases
"paths": {
"@/*": ["src/*"]
},
// ES module interoperability
"esModuleInterop": true,
// Allow default imports
"allowSyntheticDefaultImports": true,
// Isolate modules
"isolatedModules": true
}
}
ES Feature Options
Example
{
"compilerOptions": {
// Compilation target
"target": "ES2020",
// Enabled libraries
"lib": ["ES2020", "DOM"],
// Allow unused local variables
"noUnusedLocals": true,
// Allow unused parameters
"noUnusedParameters": true,
// Code downleveling
"downlevelIteration": true
}
}
"compilerOptions": {
// Compilation target
"target": "ES2020",
// Enabled libraries
"lib": ["ES2020", "DOM"],
// Allow unused local variables
"noUnusedLocals": true,
// Allow unused parameters
"noUnusedParameters": true,
// Code downleveling
"downlevelIteration": true
}
}
Experimental Options
Example
{
"compilerOptions": {
// Enable decorators
"experimentalDecorators": true,
// Enable decorator metadata
"emitDecoratorMetadata": true,
// Enable async iterators
"emitDecoratorMetadata": true,
// Skip library checks
"skipLibCheck": true
}
}
"compilerOptions": {
// Enable decorators
"experimentalDecorators": true,
// Enable decorator metadata
"emitDecoratorMetadata": true,
// Enable async iterators
"emitDecoratorMetadata": true,
// Skip library checks
"skipLibCheck": true
}
}
Common Compiler Option Combinations
Example
// Recommended configuration for Node.js projects
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"lib": ["ES2020"],
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"moduleResolution": "node",
"declaration": true
}
}
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"lib": ["ES2020"],
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"moduleResolution": "node",
"declaration": true
}
}
Compiler Options Table
| Category | Common Options |
|---|---|
| Output | outDir, rootDir, declaration, sourceMap |
| Type Checking | strict, strictNullChecks, noImplicitAny |
| Module | module, moduleResolution, paths, esModuleInterop |
| ES Features | target, lib, downlevelIteration |
| Experimental | experimentalDecorators, emitDecoratorMetadata |
Summary
- Strict mode:Always enable strict: true
- Target version:Choose the appropriate target based on the environment
- Module system:Choose module based on the deployment environment
- Type declarations:Enable declaration when developing libraries