ProgramGuru

Configuring rootDir and outDir

Configuring rootDir and outDir — why it matters

rootDir controls the common directory structure for emitted files; outDir tells TypeScript where to place generated output. rootDir does not decide which files are included in the project. Use include/exclude or files for input selection and keep generated output away from source folders.

What you’ll learn

  • Explain configuring rootdir and outdir and the problem it solves.
  • Identify the relevant compile-time checks and runtime responsibilities.
  • Run the example, predict its output, and try a realistic modification.

Understand the mechanism

The displayed mapping is a model for a typical src/main.ts file. The actual compiler performs the mapping when tsconfig.json sets rootDir and outDir.

rootDir does not exclude files outside the directory from the compilation inputs; it constrains their emitted layout.

Follow the concept flow

Configuring rootDir and outDir — concept flowsrc/main.ts → Compiler emit → dist/main.jsConfiguring rootDir and outDir1src/main.ts2Compiler emit3dist/main.jsCompiler configuration changes checks and output, not runtime guarantees.
Follow the responsibilities from input to checked result.

Run the primary TypeScript example

Save as configuring-rootdir-outdir.ts. The following program passed strict TypeScript compilation, and the displayed output was captured by executing its emitted JavaScript with Node.js 22.16.0.

TypeScript source

type BuildPaths = { rootDir: string; outDir: string };
const paths: BuildPaths = { rootDir: "src", outDir: "dist" };
console.log(`${paths.rootDir}/main.ts -> ${paths.outDir}/main.js`);

Verified output

src/main.ts -> dist/main.js

Compile: npx tsc --strict --target es2022 --module commonjs configuring-rootdir-outdir.ts; run: node configuring-rootdir-outdir.js. For Node.js API examples, install @types/node first. For browser projects, use the browser-specific instructions below.

The example demonstrates the code-level behavior; the related configuration or test commands are shown separately when needed.

Trace the result

The displayed mapping is a model for a typical src/main.ts file. The actual compiler performs the mapping when tsconfig.json sets rootDir and outDir.

Predict what changes if you alter one input. Then run the code to compare your reasoning with the real output.

Try the full tool or configuration workflow

Practical project lab: These are real configuration and source-file examples, not a TypeScript object that merely describes settings. Place the files at the named paths relative to one project directory.

tsconfig.json

{"compilerOptions":{"target":"ES2022","module":"CommonJS","strict":true,"rootDir":"src","outDir":"dist"},"include":["src/**/*.ts"]}

src/main.ts

console.log("Build layout");

Commands

npx tsc -p .
node dist/main.js

Check generated dist/main.js and keep outputs out of src.

This local compiler or test-runner mini-project was validated in addition to the primary program.

Common mistake and correction

rootDir does not exclude files outside the directory from the compilation inputs; it constrains their emitted layout.

Identify whether the failure is a type-checking issue, a configuration issue, or a runtime behavior issue before choosing a fix.

Practice the concept

Put main.ts in src/ and set outDir to dist/. Compile and inspect the generated folder.

Write down your expected result first. For configuration examples, compare compiler output and diagnostics; for test examples, make the assertion fail deliberately to verify that the tool reports it.

Check your understanding

What should you remember when configuring TypeScript compilation?
Choose one answer. You can try again.

Answer and explanation

Compiler options control static checks or emitted files; running the result is a separate step.

The displayed mapping is a model for a typical src/main.ts file. The actual compiler performs the mapping when tsconfig.json sets rootDir and outDir.

Official reference

For the exact current options and platform behavior, consult the official documentation. These examples were verified using the tool versions described above.

What to remember

rootDir controls the common directory structure for emitted files; outDir tells TypeScript where to place generated output.

Run the primary example, then complete the practice task or environment-specific lab.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Source Maps and Debugging →