TypeScript
tsconfig.json
tsconfig.json is a configuration file used by the TypeScript compiler to understand how to transpile your code. Its folder is the root of the project: running tsc there compiles the files it describes.
Create one with:
npx tsc --init
Example
tsconfig.json
{
"compilerOptions": {
"target": "ES2022",
"module": "nodenext",
"outDir": "dist",
"rootDir": "src",
"strict": true,
"noUncheckedIndexedAccess": true,
"skipLibCheck": true
},
"include": ["src"]
}
| Top-level key | Description |
|---|---|
compilerOptions | How to check and emit the code (the tables below). |
include | Files or folders to compile (glob patterns allowed). |
exclude | Files to skip from include (default: node_modules and outDir). |
extends | Inherits options from another config, e.g. a shared base file. |
Below are some of the most commonly used options grouped by category.
Core Options
| Option | Description | Example |
|---|---|---|
target | JavaScript version to output | "ES2022" |
module | Module system to use (nodenext, esnext, etc.) | "nodenext" |
moduleResolution | How imports are found (nodenext, bundler) | "bundler" |
lib | Include built-in libraries | ["ES2022", "DOM"] |
types | @types packages to load globally | ["node"] |
allowJs | Allow JavaScript files to be compiled | true |
checkJs | Enable type-checking for JS files | true |
outDir | Output directory for .js files | "dist" |
rootDir | Root directory of input .ts files | "src" |
Which
module setting? Code that runs in Node.js: "nodenext". Code bundled by Vite, webpack or similar: "esnext" with "moduleResolution": "bundler".Strictness Options
| Option | Description | Example |
|---|---|---|
strict | Enable all strict type-checking options | true |
noImplicitAny | Disallow variables with any type unless explicitly typed | true |
strictNullChecks | Disallow assigning null/undefined to other types | true |
strictFunctionTypes | Enforce function parameter type compatibility | true |
noUncheckedIndexedAccess | Index access (arr[0], obj[key]) may be undefined | true |
strict turns on a whole family of checks, including noImplicitAny, strictNullChecks and strictFunctionTypes. noUncheckedIndexedAccess is not part of it and has to be enabled separately.
Code Quality & Behavior
| Option | Description | Example |
|---|---|---|
noUnusedLocals | Warn about unused variables | true |
noUnusedParameters | Warn about unused function parameters | true |
noEmitOnError | Don't emit .js if there are type errors | true |
noImplicitOverride | Require override on methods that override a base method | true |
resolveJsonModule | Allow importing .json files | true |
skipLibCheck | Skip type checking of declaration files (.d.ts) | true |
declaration | Generate .d.ts files (for libraries) | true |
sourceMap | Generate source maps for debugging | true |
Changes in TypeScript 6.0 and 7.0
Older guides and configs may not match current behavior:
| Setting | Now |
|---|---|
strict | true by default. Set "strict": false to opt out. |
module | Defaults to esnext. amd, umd, systemjs and none are gone. |
target | Defaults to the latest ECMAScript version. ES5 is no longer supported. |
types | Defaults to []: list @types packages you need, e.g. ["node"]. |
rootDir | Defaults to the folder containing tsconfig.json. |
esModuleInterop | Always on; it can't be set to false anymore. |
moduleResolution: "node" / "classic" | Removed. Use nodenext or bundler. |
baseUrl, outFile, downlevelIteration | Removed. |
See the TypeScript 6.0 and 7.0 release notes for the full list.
