TypeScripttsconfig options
"outDir" and "rootDir"
The folder where compiled code goes, and the folder of the sources.
Example
#JSON
{
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist",
"declaration": true
},
"include": ["src"]
}Definition and usage
#The outDir option points to the folder for generated JavaScript files (and optionally .d.ts files). Without it, the compiler writes the output next to the source files, which quickly makes a mess in the project.
rootDir sets the base folder of the sources. The folder structure inside rootDir is reproduced in outDir: the file src/utils/format.ts ends up in dist/utils/format.js.
In projects with a bundler or framework (Next.js, Vite), the build tool does the compilation, and tsconfig often has noEmit: true. In that case outDir doesn’t matter.
Syntax
#Syntax
"rootDir": "./src",
"outDir": "./dist"Good practices
#- Add the
distfolder to.gitignore. Generated code shouldn’t go into the repository. - The
declaration: trueoption generates.d.tsfiles with types, which you need when publishing libraries.
Related entries
#- "include" and "exclude"Which files the compiler takes into account, and which it skips.
- "target"The JavaScript version the compiler turns your code into.
- "module"The module format of the generated code and how imports are understood.
- "declaration"Generates .d.ts files with types next to the compiled JavaScript.
Spotted a mistake or missing an example? Write to us.