tsconfig in depth

Topic 1 of 4 in Configuration and tooling8 min readIntermediateFree course

Overview

tsconfig.json controls how strict TypeScript is, which JavaScript it assumes, how it finds modules, and what (if anything) it outputs. There are over a hundred options, but only a couple of dozen matter for most projects. This lesson groups them by the question they answer, explains the defaults — several of which changed in TypeScript 6 and 7 — and shows recommended setups.

A tour of tsconfig.json, grouped by question. How strict? strict, noUncheckedIndexedAccess, exactOptionalPropertyTypes, noImplicitOverride. Which JavaScript? target, lib, module, moduleResolution. Which files? include / exclude, rootDir, outDir, types. What output? noEmit, declaration, sourceMap, declarationMap. Play well with tools: verbatimModuleSyntax, isolatedModules, erasableSyntaxOnly, skipLibCheck. extends: start from a shared base, such as “extends”: “@tsconfig/node26/tsconfig.json”. tsc –init writes a commented starting point; tsc –showConfig prints the final settings after extends and defaults.

How strict?

strict

"strict": true switches on a family of checks: noImplicitAny, strictNullChecks, strictFunctionTypes, strictBindCallApply, strictPropertyInitialization, strictBuiltinIteratorReturn, noImplicitThis, useUnknownInCatchVariables and alwaysStrict. It’s the default since TypeScript 6 — but write it explicitly anyway, so nobody has to remember. Turning it off is only for migrating old JavaScript.

Beyond strict

These aren’t part of strict, but are worth turning on in new projects:

Option Catches
noUncheckedIndexedAccess arr[i] and record[key] might be undefined
exactOptionalPropertyTypes { title?: string } may omit title, but not set it to undefined
noImplicitOverride overriding a method without override
noImplicitReturns a function where some paths don’t return
noFallthroughCasesInSwitch a non-empty case without break or return
noPropertyAccessFromIndexSignature env.PORT on an index signature — write env["PORT"] to show it may be missing
noUnusedLocals, noUnusedParameters dead code (often left to the linter instead)

The messages are specific, for example:

Text
error TS2375: Type '{ title: undefined; }' is not assignable to type 'Options' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
error TS4111: Property 'PORT' comes from an index signature, so it must be accessed with ['PORT'].
error TS7030: Not all code paths return a value.
error TS7029: Fallthrough case in switch.

tsc --init (TypeScript 7) already turns on noUncheckedIndexedAccess and exactOptionalPropertyTypes.

Which JavaScript?

  • target — which JavaScript syntax to emit. Newer syntax is rewritten for older targets (only things like ?. — TypeScript doesn’t add polyfills). Since TypeScript 6 the default is the latest stable ECMAScript version (es2025); es5 is gone. For Node 26 or modern browsers, es2025 or esnext is right.
  • lib — which built-in APIs the code may use (Array.prototype.toSorted, Promise.withResolvers, the DOM…). It defaults to match target, plus the DOM. For Node-only code, set lib without dom so browser globals like document are errors.
  • types — which global @types packages to load. Since TypeScript 6 the default is none ([]), so list them: "types": ["node"] for Node, "types": ["vite/client"] for Vite apps. (Packages you import still find their own types.)

How are modules resolved?

  • module: "nodenext" — for code Node runs (directly or after tsc). TypeScript follows Node’s ESM/CommonJS rules exactly, including required file extensions. It implies moduleResolution: "nodenext".
  • module: "esnext" + moduleResolution: "bundler" — for code a bundler builds (Vite, webpack, esbuild). Extension-less imports and package exports conditions work like the bundler expects.
  • Since TypeScript 6, module defaults to esnext. The old node/node10 and classic resolution modes, and amd/umd/system module formats, are removed in TypeScript 7.

Which files?

  • include / exclude — which files make up the project ("include": ["src"]).
  • rootDir — the root of the source tree, mirrored into outDir. Since TypeScript 6 it defaults to the folder containing tsconfig.json, so with sources in src/, set "rootDir": "src" or your output lands in dist/src/.
  • outDir — where emitted files go.

What output?

  • noEmit: true — check only. Right for apps where Node, Vite or another tool runs the TypeScript.
  • declaration — emit .d.ts files (libraries). declarationMap lets “go to definition” jump to your .ts source.
  • sourceMap — map compiled JavaScript back to TypeScript for debuggers and stack traces.

Playing well with other tools

Tools like Node, esbuild, Vite and Vitest strip types one file at a time, without the type checker. These options make TypeScript reject code those tools can’t handle correctly:

  • verbatimModuleSyntax — imports of types must be marked type (Modules).
  • isolatedModules — reject anything that needs cross-file information to compile (implied by verbatimModuleSyntax).
  • erasableSyntaxOnly — reject TypeScript syntax that generates code: enum, namespace with values, parameter properties (Enums and alternatives, Classes).
  • skipLibCheck — don’t type-check .d.ts files in node_modules; faster and avoids errors you can’t fix.

extends and shared bases

A tsconfig can start from another and override parts of it:

JSON
{
  "extends": "@tsconfig/node26/tsconfig.json",
  "compilerOptions": {
    "noEmit": true,
    "noUncheckedIndexedAccess": true
  },
  "include": ["src"]
}

The @tsconfig/bases packages (npm install -D @tsconfig/node26, @tsconfig/strictest, @tsconfig/vite-react, …) publish recommended settings per platform. @tsconfig/node26 sets target/lib to es2025, module: "nodenext", types: ["node"], esModuleInterop and skipLibCheck. Monorepos use extends to share one base across packages.

To see what actually applies after extends and defaults:

Terminal
npx tsc --showConfig

A Node.js app or script that Node runs directly (as in this course):

JSON
{
  "compilerOptions": {
    "module": "nodenext",
    "target": "esnext",
    "types": ["node"],
    "strict": true,
    "noUncheckedIndexedAccess": true,
    "noEmit": true,
    "allowImportingTsExtensions": true,
    "erasableSyntaxOnly": true,
    "verbatimModuleSyntax": true,
    "skipLibCheck": true
  },
  "include": ["src"]
}

A front-end app built by Vite: use the template’s config (npm create vite@latest), which sets moduleResolution: "bundler", jsx, noEmit and the Vite client types.

A library published to npm: see Building and publishing.

Deprecations and migration

TypeScript 7 removed options deprecated in 6.0 — target: es5, downlevelIteration, baseUrl, outFile, moduleResolution: node10/classic, module: amd/umd/system/none, setting esModuleInterop or alwaysStrict to false. If an old project fails to start on TypeScript 7 with configuration errors, upgrade it with TypeScript 6 first: 6.0 reports the same problems as warnings, and "ignoreDeprecations": "6.0" lets you migrate step by step.

Remember: be strict (and stricter), match module to how your code runs, list your global types, set rootDir when emitting, and check the final result with tsc --showConfig.

Exercise

Pass the strictest settings

The test project for this exercise turns on extra checks beyond strict: exactOptionalPropertyTypes, noImplicitReturns, noFallthroughCasesInSwitch, noImplicitOverride, noPropertyAccessFromIndexSignature and noUncheckedIndexedAccess. The starter has one problem for each. Fix the code (not the config) so it type-checks, keeping the behaviour, and add a // @ts-expect-error example showing what exactOptionalPropertyTypes rejects. The program should print dark | 8080 | medium | Woof | first.

TypeScript
type Prefs = { theme?: "light" | "dark" };
const prefs: Prefs = { theme: undefined };   // exactOptionalPropertyTypes

const env: Record<string, string> = { PORT: "8080" };
const port = env.PORT;                       // noPropertyAccessFromIndexSignature

function size(n: number): string | undefined {   // noImplicitReturns
  if (n < 10) return "small";
  if (n < 100) return "medium";
}

function label(level: number): string {
  let text = "";
  switch (level) {
    case 1:
      text = "low";                          // noFallthroughCasesInSwitch
    case 2:
      text = "high";
      break;
  }
  return text;
}

class Animal { speak(): string { return "..."; } }
class Dog extends Animal { speak(): string { return "Woof"; } }   // noImplicitOverride

const items = ["first", "second"];
const firstItem: string = items[0];          // noUncheckedIndexedAccess

console.log([prefs.theme ?? "dark", port, size(50), new Dog().speak(), firstItem, label(1)].slice(0, 5).join(" | "));

Summary

  • strict (the default since TypeScript 6) enables a family of checks; add noUncheckedIndexedAccess and friends for even more safety.
  • target and lib describe the JavaScript features available; types lists global type packages such as node.
  • module/moduleResolution must match how code runs: nodenext for Node, bundler for Vite and friends.
  • noEmit for check-only projects; outDir, rootDir, declaration and sourceMap when tsc builds output.
  • Start from a shared base with extends (e.g. @tsconfig/node26) and inspect the result with tsc --showConfig.