Skip to content

Search

Articles, tools, solutions and knowledge base guides

Start typing to search across articles, tools and apps.

TypeScript tsconfig.json: Compiler Configuration Explained

A tsconfig.json describes a TypeScript project, not the entire execution environment. This guide connects compiler choices to the actual build, type checking and output layout, with separate examples for bundled applications, directly executed Node.js output and libraries.

Published Written by Reading time 11 min read
On this page Contents: quick navigation between sections

Project boundaries

Run tsc -p tsconfig.json to select a configuration explicitly. files lists entry files, include matches patterns, and exclude limits discovery through include. Imports can still bring excluded files into the program; exclude is not an isolation boundary. rootDir controls output layout, not input selection. [1][2]

Strict type checking

browser/src/selection.ts

export interface Selection {
  label?: string;
}

export function firstLabel(labels: readonly string[]): Selection {
  const first = labels[0];

  if (first === undefined) {
    return {};
  }

  return { label: first };
}

browser/src/selection.typecheck.ts

import { firstLabel } from "./selection";
import type { Selection } from "./selection";

const empty: Selection = firstLabel([]);

// @ts-expect-error: an optional label cannot be explicitly undefined.
const invalid: Selection = { label: undefined };

const labels: string[] = [];

// @ts-expect-error: an unchecked index may be undefined.
const missing: string = labels[0];

export { empty, invalid, missing };

strict enables a group of checks including strictNullChecks and noImplicitAny. Enable noUncheckedIndexedAccess to account for undefined on indexed reads and exactOptionalPropertyTypes to distinguish an omitted property from explicit undefined. Neither validates external API data at runtime. The example handles empty arrays and models an optional label without assertions. [1][3][4]

target, lib and types

target affects JavaScript syntax lowering; lib supplies API declarations; types selects global declarations from @types packages. Adding DOM does not create document on a server, and newer lib entries do not install polyfills. Separate server, browser and test configurations; install matching @types/node and specify types: ["node"] when using Node APIs. [1][5][6]

Bundled browser applications

tsconfig.base.json

{
  "compilerOptions": {
    "strict": true,
    "noUncheckedIndexedAccess": true,
    "exactOptionalPropertyTypes": true,
    "forceConsistentCasingInFileNames": true,
    "verbatimModuleSyntax": true
  }
}

browser/tsconfig.json

{
  "extends": "../tsconfig.base.json",
  "compilerOptions": {
    "target": "ES2022",
    "lib": [
      "ES2022",
      "DOM",
      "DOM.Iterable"
    ],
    "types": [],
    "module": "ESNext",
    "moduleResolution": "Bundler",
    "isolatedModules": true,
    "noEmit": true
  },
  "include": [
    "src/**/*.ts"
  ]
}

For Vite or another bundler, ESNext modules, Bundler resolution and noEmit: true make a useful starting point. isolatedModules flags constructs that are problematic for single-file transformation, while verbatimModuleSyntax distinguishes type imports from value imports. The bundler must enforce the actual output compatibility; add jsx: react-jsx for a React project when it matches the chosen JSX pipeline. [1][7][8]

Node.js and ESM

node/package.json

{
  "type": "module",
  "private": true
}

node/tsconfig.json

{
  "extends": "../tsconfig.base.json",
  "compilerOptions": {
    "target": "ES2022",
    "lib": [
      "ES2022"
    ],
    "types": [],
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "rootDir": "./src",
    "outDir": "./dist",
    "noEmitOnError": true,
    "sourceMap": true
  },
  "include": [
    "src/**/*.ts"
  ]
}

node/src/format.ts

export function formatLabel(label: string): string {
  return label.trim();
}

node/src/index.ts

import { formatLabel } from "./format.js";

export const label = formatLabel(" TypeScript ");

For JavaScript emitted by tsc and run directly by modern Node.js, use NodeNext for both module and moduleResolution. A package.json with type: module marks .js files as ESM. A ./format.js import in a .ts file resolves to the TypeScript source for checking and the emitted JavaScript at runtime. Set a supported Node version and execute the result, because NodeNext evolves with the platform. [7][8][9]

JavaScript and declaration output

library/tsconfig.json

{
  "extends": "../tsconfig.base.json",
  "compilerOptions": {
    "target": "ES2022",
    "lib": [
      "ES2022"
    ],
    "types": [],
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "rootDir": "./src",
    "outDir": "./dist",
    "declaration": true,
    "declarationMap": true,
    "noEmitOnError": true
  },
  "include": [
    "src/**/*.ts"
  ]
}

library/package.json

{
  "name": "example-labels",
  "version": "1.0.0",
  "type": "module",
  "exports": {
    ".": {
      "types": "./dist/index.d.ts",
      "import": "./dist/index.js"
    }
  },
  "files": [
    "dist"
  ]
}

library/src/index.ts

export function normalizeLabel(label: string): string {
  return label.trim().toLowerCase();
}

noEmit prevents JavaScript and declaration output; noEmitOnError blocks emission when errors occur. outDir groups output and rootDir controls its layout. For libraries, declaration generates .d.ts, declarationMap enables source navigation, and emitDeclarationOnly is useful when another tool emits JavaScript. A declaration is not an implementation; test the published package exports as well. [1][8]

Path aliases and runtime resolution

alias/tsconfig.json

{
  "extends": "../browser/tsconfig.json",
  "compilerOptions": {
    "paths": {
      "@app/*": [
        "./src/*"
      ]
    }
  },
  "include": [
    "src/**/*.ts"
  ]
}

alias/src/label.ts

export const label = "TypeScript";

alias/src/index.ts

import { label } from "@app/label";

export { label };

paths changes TypeScript's module lookup, not the import specifiers in emitted JavaScript. The bundler, runtime and test runner must agree on aliases. For libraries, prefer real package dependencies and exports instead of aliases that reach into another package's internal source and bypass its public API. [9][10]

Configuration inheritance and monorepos

monorepo/tsconfig.json

{
  "files": [],
  "references": [
    {
      "path": "./core"
    },
    {
      "path": "./app"
    }
  ]
}

monorepo/core/package.json

{
  "type": "module",
  "private": true
}

monorepo/core/tsconfig.json

{
  "extends": "../../tsconfig.base.json",
  "compilerOptions": {
    "target": "ES2022",
    "lib": [
      "ES2022"
    ],
    "types": [],
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "rootDir": "./src",
    "outDir": "./dist",
    "composite": true,
    "declaration": true,
    "noEmitOnError": true
  },
  "include": [
    "src/**/*.ts"
  ]
}

monorepo/core/src/index.ts

export function normalizeLabel(label: string): string {
  return label.trim();
}

monorepo/app/package.json

{
  "type": "module",
  "private": true
}

monorepo/app/tsconfig.json

{
  "extends": "../../tsconfig.base.json",
  "compilerOptions": {
    "target": "ES2022",
    "lib": [
      "ES2022"
    ],
    "types": [],
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "rootDir": "./src",
    "outDir": "./dist",
    "composite": true,
    "declaration": true,
    "noEmitOnError": true
  },
  "include": [
    "src/**/*.ts"
  ],
  "references": [
    {
      "path": "../core"
    }
  ]
}

monorepo/app/src/index.ts

import { normalizeLabel } from "../../core/dist/index.js";

export const label = normalizeLabel(" TypeScript ");

extends shares compiler options. Relative paths refer to the config where they are declared; child files, include and exclude replace the corresponding base values. references is not inherited. Use references and composite to let tsc -b build the dependency graph and declarations. Do not put package-specific rootDir and outDir in a shared base. [1][11]

Migrating to TypeScript 6.0

In 6.0, strict is enabled by default, target follows the latest supported standard by default, module defaults to esnext, types defaults to an empty list and rootDir defaults to the config directory. baseUrl is deprecated; paths works without it. Keep compatibility settings explicit, check migration notes and remove obsolete options rather than permanently suppressing warnings. [6]

Diagnostics, CI and tests

commands.sh

npx tsc -p browser/tsconfig.json --showConfig
npx tsc -p browser/tsconfig.json --explainFiles
npx tsc -p browser/tsconfig.json --traceResolution
npx tsc -p browser/tsconfig.json --extendedDiagnostics
npx tsc -p node/tsconfig.json
node node/dist/index.js
npx tsc -b monorepo/tsconfig.json

Pin TypeScript in project dependencies and use the local compiler. showConfig reveals the effective configuration, explainFiles explains file inclusion, traceResolution diagnoses imports and extendedDiagnostics measures compilation costs. Migrate JavaScript gradually using allowJs and checkJs. skipLibCheck skips checking declaration files; it does not fix dependency conflicts. CI should check types, build the actual artifact and execute emitted Node.js code. [1][2][11]

Choose settings to match the execution environment and emitter. Maintain strict checking, explicit project boundaries and reproducible CI commands. Compiler options cannot replace runtime tests or data validation.

Tools for this topic

TypeScript tsconfig.json Compiler Options Node.js ESM Monorepo Type Safety Module Resolution

Frequently asked questions

Is strict sufficient?

No. Enable noUncheckedIndexedAccess and exactOptionalPropertyTypes separately and validate external data. [3][4]

Does exclude block imports?

No. An import can still add an excluded file to the program. [1]

Does target install polyfills?

No. Syntax conversion and runtime APIs are separate concerns. [5]

Bundler or NodeNext?

Bundler for bundled code; NodeNext for emitted modules run directly in Node. [7][8]

Do paths aliases work in Node without configuration?

Not automatically. TypeScript does not rewrite specifiers based on paths. [10]

Why use noEmit?

To check types without generating JavaScript and declarations when another tool handles emission. [1]

How can I inspect the effective configuration?

Run tsc -p tsconfig.json --showConfig. [2]

Does skipLibCheck safely fix conflicting types?

No. It reduces declaration checking, without removing the underlying conflict. [1]

What does composite do?

It imposes constraints that support efficient referenced-project builds, including declaration generation. [11]

Does TypeScript 6.0 require baseUrl for paths?

No. paths has long worked without baseUrl, which is deprecated in 6.0. [6][10]

Sources and references

  1. TypeScript, TSConfig Reference↩1↩2↩3↩4↩5↩6↩7↩8↩9↩10
  2. TypeScript, tsc CLI Options↩1↩2↩3
  3. TypeScript, noUncheckedIndexedAccess↩1↩2↩3
  4. TypeScript, exactOptionalPropertyTypes↩1↩2↩3
  5. TypeScript, lib↩1↩2↩3
  6. TypeScript, 6.0 Release Notes↩1↩2↩3↩4
  7. TypeScript, moduleResolution↩1↩2↩3↩4
  8. TypeScript, Choosing Compiler Options↩1↩2↩3↩4↩5
  9. TypeScript, Modules Reference↩1↩2
  10. TypeScript, paths↩1↩2↩3↩4
  11. TypeScript, Project References↩1↩2↩3

Was this helpful?

Get new articles by email

One short email per new Learning article. No spam, unsubscribe in one click.

We only use your email to send new articles. No third-party sharing.

Back to Learning