TypeScript Compiler Options
The TypeScript compiler (tsc) has many compiler options; this tutorial introduces commonly used options and their effects in detail.
Output control options
Example
{
"compilerOptions"
:
{
// Output directory
"outDir"
:
"./dist"
,
// Root directory of source code
"rootDir"
:
"./src"
,
// Generate .d.ts declaration files
"declaration"
:
true
,
// Declaration file output directory
"declarationDir"
:
"./types"
,
// Generate a source map
"sourceMap"
:
true
,
// Generate a .js.map file
"mapRoot"
:
"./map"
}
}
Type checking options
Example
{
"compilerOptions"
:
{
// Strict mode (recommended to always enable)
"strict"
:
true
,
// Check null and undefined
"strictNullChecks"
:
true
,
// Check the this parameter
"noImplicitThis"
:
true
,
// Strict function types
"strictFunctionTypes"
:
true
,
// Strict property initialization
"strictPropertyInitialization"
:
true
,
// Disallow implicit any
"noImplicitAny"
:
true
,
// Do not allow 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 interop
"esModuleInterop"
:
true
,
// Allow default imports
"allowSyntheticDefaultImports"
:
true
,
// Isolated 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
}
}
Experimental options
Example
{
"compilerOptions"
:
{
// Enable decorators
"experimentalDecorators"
:
true
,
// Enable decorator metadata
"emitDecoratorMetadata"
:
true
,
// Enable async iterators
"emitDecoratorMetadata"
:
true
,
// Skip library checking
"skipLibCheck"
:
true
}
}
Common combinations of compiler options
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
}
}
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 an appropriate target for the environment
- **Module system:** choose module according to the deployment environment
- **Type declarations:** enable declaration when developing a library