TypeScript
Emit options
Learn outDir, noEmit, and emit options.
What are emit options?
Emit options determine which files TypeScript produces after compilation. outDir sets the output directory, noEmit disables output entirely, and emitDeclarationOnly produces only .d.ts files. These settings are the primary way to control build output.
{
"compilerOptions": {
"outDir": "dist",
"noEmit": false
}
}
noEmit: true is used for type-checking only; it produces no output.
outDir and rootDir
outDir sets the directory where compiled files are written. rootDir defines the root of source files; this makes the output directory structure mirror the source structure. Used together, the output path is built under outDir relative to rootDir.
{
"compilerOptions": {
"rootDir": "src",
"outDir": "dist"
}
}
// src/index.ts → dist/index.js
If rootDir is not specified, TypeScript infers it automatically.
Exercise Compute the output path
Write a computeOutputPath function taking sourcePath, rootDir, and outDir. Strip the rootDir prefix from the source path, prepend the outDir prefix, and change the .ts extension to .js. Print the result of computeOutputPath("src/index.ts", "src", "dist").
Show example solution
function computeOutputPath(sourcePath: string, rootDir: string, outDir: string): string {
const relative = sourcePath.startsWith(rootDir + "/")
? sourcePath.slice(rootDir.length + 1)
: sourcePath;
const withJs = relative.replace(/\.ts$/, ".js");
return outDir + "/" + withJs;
}
console.log(computeOutputPath("src/index.ts", "src", "dist"));
Test yourself
-
What does `noEmit: true` do?
- Produces no files, only type-checks
- Enables source map generation
- Changes the output directory
- Produces only .d.ts files
Show answer
Produces no files, only type-checks — noEmit: true disables output entirely and only type-checks; outDir changes the directory, emitDeclarationOnly produces only .d.ts, and sourceMap generates maps.
-
Set the output directory
{
"compilerOptions": {
"____": "dist"
}
}
Show answer
{
"compilerOptions": {
"outDir": "dist"
}
}
-
Emit parts
- outDir
- sets the output directory
- rootDir
- sets the source root directory
- noEmit
- disables output entirely
- emitDeclarationOnly
- produces only .d.ts files
Next lesson: Source map