All academies
Roadmap/Expert

Level 17

Configuration (tsconfig)

tsconfig.json controls what TypeScript accepts, what JavaScript it emits, and how modules resolve — understanding strict flags, target, module, and paths turns mysterious build behavior into deliberate choices.

25 min read + quiz

The Anatomy of tsconfig.json

A tsconfig.json marks the root of a TypeScript project and tells the compiler three things: which files belong to the project (include, exclude, files), how to check them (strictness flags), and what to emit (target, module, outDir). Editors use the same file, so it defines the red squiggles you see, not just the build.

Projects commonly share settings via extends: a base config holds the strictness policy, and per-package configs override only emit details. Monorepos take this further with project references, which give each package its own compilation unit and enable incremental cross-package builds.

One underappreciated fact: compiler options only apply to files in the project. A file that is not matched by include is checked with default (loose!) settings when you open it, which is why a stray script sometimes shows different errors than the rest of the codebase.

// tsconfig.json (annotated excerpt)
// {
//   "compilerOptions": {
//     "strict": true,          // the master switch (see next section)
//     "target": "ES2022",      // JS features assumed available at runtime
//     "module": "NodeNext",    // module system + resolution strategy
//     "outDir": "dist",
//     "sourceMap": true
//   },
//   "include": ["src"],
//   "exclude": ["node_modules", "dist"]
// }

The three concerns: which files, how strict, what output.

strict: The Flag Family That Matters Most

strict: true is an umbrella that enables a family of checks; the most impactful are strictNullChecks (null and undefined are no longer assignable to everything), noImplicitAny (parameters must have inferable or declared types), and strictFunctionTypes (function parameters are checked contravariantly, closing a soundness hole).

strictNullChecks alone changes the language. Without it, string secretly means string | null | undefined and every null-related crash type-checks fine. With it, optionality becomes visible in types and the compiler forces handling. If you adopt only one flag from this lesson, adopt this one.

New projects should simply start with strict: true — it is free on day one and expensive to retrofit. For legacy migrations, enable sub-flags incrementally (noImplicitAny first, then strictNullChecks), and consider noUncheckedIndexedAccess as a valuable extra even beyond strict: it makes arr[i] type as T | undefined, reflecting the truth that index access can miss.

// With "strictNullChecks": false — this compiles and crashes:
// function greet(name: string) { return name.toUpperCase(); }
// greet(null); // runtime: TypeError

// With "strictNullChecks": true:
function greet(name: string | null): string {
  if (name === null) {
    return "Hello, stranger";
  }
  return "Hello, " + name.toUpperCase();
}

// With "noUncheckedIndexedAccess": true:
const list: string[] = ["a"];
const item = list[5]; // item: string | undefined — honest!
console.log(item?.toUpperCase() ?? "(missing)");

Strict flags surface at compile time what would otherwise crash at runtime.

target: What JavaScript You Emit

target sets which JavaScript version the output assumes: with target ES5, classes compile to prototype functions and async/await into state-machine helpers; with ES2022, they pass through untouched. It also selects the built-in type definitions via lib — Array.prototype.at only type-checks when your lib includes ES2022 or later.

Choose target from your actual runtimes. Modern Node (18+) and evergreen browsers comfortably support ES2022. Compiling lower than needed bloats output and slows it down; compiling higher than your oldest supported runtime causes syntax errors in production — the worst kind of discovery.

target does not add missing runtime APIs. It downlevels syntax, but if your code calls Array.prototype.at on an old runtime, you need a polyfill regardless of target. The lib setting can even lie on purpose — declaring APIs you promise to polyfill — so keep lib and your polyfill story in sync.

// Same source, two targets:

// source
class Point {
  constructor(public x: number, public y: number) {}
}

// target ES2022 output (essentially unchanged):
// class Point { constructor(x, y) { this.x = x; this.y = y; } }

// target ES5 output (downleveled):
// var Point = (function () {
//   function Point(x, y) { this.x = x; this.y = y; }
//   return Point;
// })();

target rewrites syntax — it never installs missing runtime APIs.

module and Module Resolution

module controls the module syntax of the output (ESM import/export vs CommonJS require) and, in modern configs, implies how imports are resolved. For Node projects, module NodeNext follows Node's real rules: package.json 'type', the exports field, and mandatory file extensions in relative ESM imports.

For code that a bundler compiles (Vite, webpack, Next.js), the right choice is usually module ESNext with moduleResolution bundler, which accepts extension-less imports and package exports the way bundlers do. The recurring mistake is mixing regimes — Node-style config for bundled code or vice versa — producing imports that type-check but fail at runtime, or the reverse.

esModuleInterop and allowSyntheticDefaultImports smooth over CommonJS/ESM friction, letting you write import express from 'express' for a CommonJS package. Modern presets enable them; if you see 'module can only be default-imported using esModuleInterop', that is the flag to check.

// "module": "NodeNext" — Node ESM rules apply:
// import { helper } from "./utils.js";  // extension REQUIRED
//                                       // (yes, .js — even from a .ts file)

// "moduleResolution": "bundler" — bundler rules apply:
// import { helper } from "./utils";     // extension-less is fine

// Rule of thumb:
//   Runs directly in Node  -> "module": "NodeNext"
//   Compiled by a bundler  -> "module": "ESNext" + "moduleResolution": "bundler"

Match resolution to whoever actually loads your modules.

paths: Import Aliases and Their Catch

The paths option maps import specifiers to locations, enabling aliases like @typescript/components/Button instead of ../../../components/Button. Aliases keep imports stable when files move and make deep hierarchies readable. baseUrl (or relative entries in paths) anchors the mapping.

The catch: paths only affects type checking. tsc does not rewrite the specifiers in emitted JavaScript, so plain-Node projects will crash at runtime with 'Cannot find module @typescript/...' even though everything compiled. Whatever runs your code — a bundler, tsconfig-paths, or Node subpath imports via package.json — must understand the same aliases.

Frameworks like Next.js and Vite read tsconfig paths (natively or via a plugin) and handle this for you, which is why the problem usually surfaces the first time someone extracts a script or library out of the framework. Keep aliases few and meaningful: one @typescript/ for src plus perhaps a #tests alias beats a zoo of single-purpose prefixes.

// tsconfig.json
// {
//   "compilerOptions": {
//     "baseUrl": ".",
//     "paths": {
//       "@typescript/*": ["src/*"]
//     }
//   }
// }

// Now this works in the type checker:
// import { Button } from "@typescript/components/Button";

// But remember: tsc emits the specifier UNCHANGED.
// Your bundler or runtime must resolve "@typescript/" too.

paths is a type-checker feature; the runtime needs matching config.

Try it yourself

This lesson's example is preloaded in the playground — break it, fix it, extend it.

Open in playground

Check your understanding

Question 1 · Multiple choice

What does "strict": true actually do?

Question 2 · True or false

Setting "target": "ES5" makes modern APIs like Array.prototype.at work on old runtimes.

Question 3 · Find the error

This project compiles cleanly with tsc but crashes when run directly with Node: Error: Cannot find module '@typescript/utils/math'. What is wrong?

// tsconfig.json
// { "compilerOptions": { "baseUrl": ".", "paths": { "@typescript/*": ["src/*"] } } }

// src/index.ts
// import { add } from "@typescript/utils/math";
// console.log(add(2, 3));

Question 4 · Multiple choice

Your code is compiled and served by Vite. Which module settings fit best?

Question 5 · Predict the output

With "noUncheckedIndexedAccess": true, what is the type of item, and what does this print?

const colors: string[] = ["red", "green"];
const item = colors[10];
console.log(item ?? "fallback");