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]
