tsconfig in depth
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.
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:
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);es5is gone. For Node 26 or modern browsers,es2025oresnextis right.lib— which built-in APIs the code may use (Array.prototype.toSorted,Promise.withResolvers, the DOM…). It defaults to matchtarget, plus the DOM. For Node-only code, setlibwithoutdomso browser globals likedocumentare errors.types— which global@typespackages to load. Since TypeScript 6 the default is none ([]), so list them:"types": ["node"]for Node,"types": ["vite/client"]for Vite apps. (Packages youimportstill find their own types.)
How are modules resolved?
module: "nodenext"— for code Node runs (directly or aftertsc). TypeScript follows Node’s ESM/CommonJS rules exactly, including required file extensions. It impliesmoduleResolution: "nodenext".module: "esnext"+moduleResolution: "bundler"— for code a bundler builds (Vite, webpack, esbuild). Extension-less imports and packageexportsconditions work like the bundler expects.- Since TypeScript 6,
moduledefaults toesnext. The oldnode/node10andclassicresolution modes, andamd/umd/systemmodule 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 intooutDir. Since TypeScript 6 it defaults to the folder containing tsconfig.json, so with sources insrc/, set"rootDir": "src"or your output lands indist/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.tsfiles (libraries).declarationMaplets “go to definition” jump to your.tssource.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 markedtype(Modules).isolatedModules— reject anything that needs cross-file information to compile (implied byverbatimModuleSyntax).erasableSyntaxOnly— reject TypeScript syntax that generates code:enum,namespacewith values, parameter properties (Enums and alternatives, Classes).skipLibCheck— don’t type-check.d.tsfiles innode_modules; faster and avoids errors you can’t fix.
extends and shared bases
A tsconfig can start from another and override parts of it:
{
"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:
npx tsc --showConfigRecommended starting points
A Node.js app or script that Node runs directly (as in this course):
{
"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
moduleto how your code runs, list your globaltypes, setrootDirwhen emitting, and check the final result withtsc --showConfig.
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.
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(" | "));type Prefs = { theme?: "light" | "dark" };
const prefs: Prefs = {}; // omit the property instead of setting undefined
// @ts-expect-error -- with exactOptionalPropertyTypes, "optional" means "may be missing", not "may be undefined"
const explicit: Prefs = { theme: undefined };
const env: Record<string, string> = { PORT: "8080" };
const port = env["PORT"] ?? "3000"; // index-signature access uses brackets (and may be missing)
function size(n: number): string {
if (n < 10) return "small";
if (n < 100) return "medium";
return "large"; // every path returns
}
function label(level: number): string {
let text = "";
switch (level) {
case 1:
text = "low";
break; // no accidental fallthrough
case 2:
text = "high";
break;
}
return text;
}
class Animal { speak(): string { return "..."; } }
class Dog extends Animal { override speak(): string { return "Woof"; } }
const items = ["first", "second"];
const firstItem: string = items[0] ?? "none";
console.log([prefs.theme ?? "dark", port, size(50), new Dog().speak(), firstItem].join(" | "));Summary
strict(the default since TypeScript 6) enables a family of checks; addnoUncheckedIndexedAccessand friends for even more safety.targetandlibdescribe the JavaScript features available;typeslists global type packages such asnode.module/moduleResolutionmust match how code runs:nodenextfor Node,bundlerfor Vite and friends.noEmitfor check-only projects;outDir,rootDir,declarationandsourceMapwhen tsc builds output.- Start from a shared base with
extends(e.g.@tsconfig/node26) and inspect the result withtsc --showConfig.