outDir and rootDir: Organizing Output
What you'll learn: How to keep your compiled JavaScript files separate from your TypeScript source code using two essential compiler options.
The Problem: Mixed Files
When TypeScript compiles your .ts files to .js files, by default it places the JavaScript output right next to the source files. This creates clutter—your project folder becomes a jumbled mix of TypeScript source and compiled JavaScript.
The Solution: Dedicated Directories
Think of your project like a factory: you want a clear separation between your workshop (source code) and the warehouse (output files).
outDir: Where Compiled Files Go
The outDir option tells TypeScript where to put all compiled JavaScript files:
{
"compilerOptions": {
"outDir": "./dist"
}
}
Now all .js files land in a dist folder, keeping them separate from your source.
rootDir: Your Source Code Home
The rootDir option specifies where your TypeScript source files live:
{
"compilerOptions": {
"outDir": "./dist",
"rootDir": "./src"
}
}
This tells TypeScript: "All my source code starts in src, so mirror that structure inside dist."
Why Both Matter Together
Without rootDir, TypeScript guesses where your source files begin, which can create unexpected folder nesting in your output. By setting both options explicitly, you control the exact structure: src/index.ts becomes dist/index.js, src/utils/helper.ts becomes dist/utils/helper.js.
Key Takeaway: Use outDir to keep compiled JavaScript separate from your source code, and rootDir to ensure your output folder mirrors your intended source structure cleanly.