Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

TypeScript Compiler Options

TypeScript Compiler Options

The TypeScript compiler (tsc) has many compiler options; this tutorial introduces commonly used options and their effects in detail.

The TypeScript compilation process and compiler options TypeScript source code .ts / .tsx Includes type annotations TypeScript compiler (tsc) Effects of compiler options: • Type checking (strict) • Output format (module) • Target version (target) • Declaration files (declaration) ...more options Compilation output .js - JavaScript .d.ts - type declarations .map - Source Map Categories of compiler options Type checking strict noImplicitAny strictNullChecks Output control outDir declaration sourceMap Module System module moduleResolution esModuleInterop Language Characteristics target lib jsx

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