TExcelTAD
Home › TypeScript › TS tsconfig.json

TypeScript tsconfig.json

A tsconfig.json file marks the root of a TypeScript project. When you run tsc without arguments, the compiler looks for this file, reads which files belong to the project and which compiler options to use, and then checks and builds everything in one go. Editors read the same file, so the red squiggles you see match what tsc reports.

Creating one

bash · try it yourself
npx tsc --init
Created a new tsconfig.json

You can learn more at https://aka.ms/tsconfig

The generated file is long and full of comments. A practical starting point for a modern project is much shorter:

json
{
  "compilerOptions": {
    "target": "es2022",
    "module": "nodenext",
    "lib": ["es2023"],
    "strict": true,
    "outDir": "dist",
    "rootDir": "src",
    "sourceMap": true,
    "declaration": true,
    "skipLibCheck": true,
    "noUncheckedIndexedAccess": true,
    "verbatimModuleSyntax": true
  },
  "include": ["src"],
  "exclude": ["node_modules", "dist"]
}

Note: tsconfig.json allows comments and trailing commas, unlike plain JSON.

Top-level fields

Field Purpose
compilerOptions how to check and emit code (most settings live here)
include glob patterns of files in the project, e.g. ["src"]
exclude patterns to skip (only filters what include found)
files an explicit list of files, for tiny projects
extends inherit options from another config, e.g. "@tsconfig/node22/tsconfig.json"
references link to other projects in a monorepo (tsc --build)

The most important compiler options

Option What it controls
target which JavaScript version is emitted (es2022 keeps classes, async, ?. as is)
module / moduleResolution module system and how import paths are resolved (nodenext for Node, bundler for Vite/webpack)
lib which built-in APIs exist: es2023, dom, …
strict turns on the whole family of strict checks (always use it)
outDir / rootDir where emitted files go and the source root they mirror
noEmit only type-check (when a bundler does the compiling)
sourceMap / declaration emit .js.map / .d.ts files
jsx how .tsx is compiled, e.g. react-jsx
paths import aliases such as @/utils

What strict turns on

"strict": true is a shorthand for several checks. The playgrounds on this site use it, which is why the errors below appear:

Flag Catches
noImplicitAny parameters and variables whose type silently becomes any
strictNullChecks using a value that might be null/undefined
strictFunctionTypes unsafe function parameter types
strictPropertyInitialization class fields never assigned
useUnknownInCatchVariables catch (err) gives unknown, not any
noImplicitThis, alwaysStrict, strictBindCallApply, … other common mistakes

Here are three of them in action in one snippet:

ts · try it yourself
function double(x) {
  return x * 2;
}

class Session {
  user: string;
}

const tags = new Map<string, string[]>();
console.log(tags.get("news").length);
main.ts(1,17): error TS7006: Parameter 'x' implicitly has an 'any' type.
main.ts(6,3): error TS2564: Property 'user' has no initializer and is not definitely assigned in the constructor.
main.ts(10,13): error TS2532: Object is possibly 'undefined'.

Each fix makes the code honest about its types:

ts · try it yourself
function double(x: number) {
  return x * 2;
}

class Session {
  user: string;
  constructor(user: string) {
    this.user = user;
  }
}

const tags = new Map<string, string[]>([["news", ["ts", "web"]]]);
console.log(double(21), new Session("mai").user, tags.get("news")?.length ?? 0);
42 mai 2

Extra safety: noUncheckedIndexedAccess

Even in strict mode, arr[i] is typed as the element type, although the index may be out of range. noUncheckedIndexedAccess (not part of strict) adds | undefined to indexed reads, forcing you to handle the missing case. Without the flag, this compiles and prints a surprise:

ts · try it yourself
const scores: number[] = [9, 7];
const third = scores[2];        // type: number (but really undefined)
try {
  console.log(third.toFixed(1));
} catch (err) {
  console.log("crashed with", (err as Error).name);
}
crashed with TypeError

The checker is happy, but the program crashes at runtime: scores[2] is undefined. With "noUncheckedIndexedAccess": true in your tsconfig.json, third would be typed number | undefined and the call would be reported as error TS18048: 'third' is possibly 'undefined' before the code ever ran. The fix is the same either way — handle the missing element:

ts · try it yourself
const scores: number[] = [9, 7];
const third = scores[2];
console.log(third !== undefined ? third.toFixed(1) : "no third score");
no third score

Useful commands

bash
npx tsc                 # build using ./tsconfig.json
npx tsc -p tsconfig.build.json   # use a different config file
npx tsc --watch         # rebuild on every save
npx tsc --showConfig    # print the final, merged configuration

A type-check-only run prints nothing when everything is fine, so scripts often add a message:

bash · try it yourself
npx tsc --noEmit && echo "No type errors"
No type errors

Tip: Start every new project with strict: true. Turning it on later in a large codebase means fixing hundreds of errors at once.

Test yourself

Which option makes tsc type-check without writing any output files?

  1. noEmit
  2. skipLibCheck
  3. outDir
  4. isolatedModules

Which check is part of strict?

  1. noUnusedLocals
  2. strictNullChecks
  3. noUncheckedIndexedAccess
  4. exactOptionalPropertyTypes

What does the lib option control?

  1. Which npm packages are installed
  2. Which built-in APIs (e.g. ES2023, DOM) the checker knows about
  3. The output folder
  4. The JavaScript version emitted