Emit options
- TypeScript course
- Expert
- Lesson 18/35
- 4 min
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.jsIf rootDir is not specified, TypeScript infers it automatically.
Practice 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").
Starter code
// computeOutputPath: kaynak yolundan çıktı yolunu üretir
// rootDir önekini çıkar
// outDir önekini ekle
// .ts uzantısını .js'ye çevir
// "src/index.ts", "src", "dist" için sonucu yazdırSample 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"));Expected output
dist/index.js
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does `noEmit: true` do?
- Produces no files, only type-checks Correct answer
- Enables source map generation
- Changes the output directory
- Produces only .d.ts files
Explanation: noEmit: true disables output entirely and only type-checks; outDir changes the directory, emitDeclarationOnly produces only .d.ts, and sourceMap generates maps.
2 Set the output directory
{
"compilerOptions": {
"____": "dist"
}
}
{
"compilerOptions": {
"outDir": "dist"
}
}
Explanation: outDir sets the output directory; rootDir is the source root, noEmit disables output, and emit is not a valid option name.
3 Emit parts
| Term | Meaning |
|---|---|
outDir |
sets the output directory |
rootDir |
sets the source root directory |
noEmit |
disables output entirely |
emitDeclarationOnly |
produces only .d.ts files |