Sub topic 5
Controlling emitted output
This chapter assumes you can run npx tsc and read a basic tsconfig.json. It also assumes you know which files include selects. Here you will control what the compiler writes and where it writes it.
What the compiler emits
For each input .ts file, tsc can produce up to four kinds of output:
| Output | Produced by | Purpose |
|---|---|---|
| `.js` file | default behavior | The code the runtime executes |
| `.js.map` file | `sourceMap` | Maps running JavaScript back to your TypeScript lines |
| `.d.ts` file | `declaration` | Type information for consumers of your library |
| `.d.ts.map` file | `declarationMap` | Lets an editor jump from a `.d.ts` to the original `.ts` |
outDir and rootDir
Without outDir, tsc writes each .js file next to its .ts source. That mixes generated files into your source tree, so most projects set outDir.
rootDir defines which folder is the top of your source tree. The compiler reproduces the structure below rootDir inside outDir. Take this layout:
src/index.ts
src/util/math.ts
dist/ (created by tsc)With "rootDir": "src" and "outDir": "dist", the output is:
dist/index.js
dist/util/math.jsIf you omit rootDir, TypeScript infers it as the longest folder path shared by all input files. That inference causes a common surprise. Suppose include lists both src and tests. The shared folder is then the project root, and your output becomes dist/src/index.js and dist/tests/index.test.js. Setting rootDir explicitly keeps the layout stable. It also produces an error if a file outside rootDir is pulled into the program, which tells you about a stray import early.
Here is a configuration that sets all the options covered so far:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"strict": true,
"rootDir": "src",
"outDir": "dist",
"sourceMap": true,
"declaration": true
},
"include": ["src"]
}outDir and rootDir are paths in the configuration file, so they resolve relative to the tsconfig.json that contains them. They do not resolve relative to the folder where you ran tsc.
Checking without emitting: noEmit
noEmit runs the full type check but writes no files. Use it in two situations:
- Continuous integration and pre-commit checks. You only want to know whether the code type-checks.
- Another tool does the compiling. Bundlers and runners such as esbuild, Vite, or a Babel setup strip types without checking them. They are fast but ignore type errors. Running
tsc --noEmitalongside them provides the checking.
You can set it in the config file or pass it as a flag for a single run:
npx tsc --noEmitA related option is noEmitOnError. By default tsc still writes JavaScript even when it reports type errors, and it exits with a non-zero status. With "noEmitOnError": true, nothing is written if any error exists. This is safer for build pipelines that deploy dist, because a broken build cannot leave stale or partly correct output behind.
Source maps
When your program throws, the stack trace points at lines in the generated .js. Those lines do not match your .ts files. A source map is a file that records the mapping. With "sourceMap": true, tsc writes greet.js.map and appends a //# sourceMappingURL=greet.js.map comment to greet.js.
Debuggers and browser developer tools read the comment automatically. Node.js does not apply maps to stack traces unless you ask it to:
node --enable-source-maps dist/index.jsTwo related options exist. inlineSourceMap embeds the map in the .js file as a data URL, which gives one file per module but a larger file. inlineSources includes your original TypeScript text in the map, so a debugger works even when src is not deployed. Do not combine sourceMap with inlineSourceMap, because the compiler rejects that pair.
Declaration files
A .d.ts file contains only types, with no runtime code. If you publish a library as JavaScript, consumers need .d.ts files to get type checking and autocomplete. Setting "declaration": true generates them. For this source:
export function greet(name: string): string {
return `Hello, ${name}`;
}the compiler writes greet.js and a greet.d.ts containing:
export declare function greet(name: string): string;Several details matter in practice:
- Exported types must be nameable. If an exported function returns a type that the compiler cannot write out, you get an error such as TS4058 or TS4023. The fix is to export that type or add an explicit return type annotation.
emitDeclarationOnlywrites only.d.tsfiles. It suits setups where a bundler produces the JavaScript andtscsupplies the types.declarationMapwrites.d.ts.mapfiles. With them, "Go to Definition" in a consumer's editor lands in your.tssource instead of the.d.ts. This only helps if the sources are shipped too.declarationDirputs the.d.tsfiles in a different folder from the.jsfiles. Most projects leave it unset.
Other options that change the output
removeCommentsdrops comments from the output, apart from/*! ... */license headers in some cases.importHelpersimports helper functions such as__awaiterfrom thetslibpackage, instead of copying them into every file. It matters when you compile to an oldtarget.incrementalsaves a.tsbuildinfocache file so repeated runs only re-check changed files. The cache goes inoutDirunless you settsBuildInfoFile.
Edge cases to keep in mind
allowJsandoutDir. If you compile JavaScript files too, they are emitted tooutDir. Without anoutDirthe compiler refuses to overwrite the input files and reports an error..d.tsinputs are not copied. Declaration files you write by hand insrcare not emitted tooutDir. If your published package needs them, copy them in a build step.- Non-TypeScript assets such as
.json(withresolveJsonModule) are emitted only when imported. Other assets like.cssor.pngare never copied. - Excluding
outDirfrom input. The defaultexcludealready contains youroutDir. If you setexcludeyourself, adddistto it. Otherwise generated.d.tsfiles can feed back into the next compile.
You can verify what a build produced with the flags below. --listEmittedFiles prints every file written. --showConfig prints the resolved settings, including the outDir path.
npx tsc --listEmittedFiles
npx tsc --showConfigExercise
Your project has src/index.ts and tests/index.test.ts, include lists both folders, outDir is dist, and no rootDir is set. Where does src/index.ts end up, and what single option change makes it land at dist/index.js instead?
Show answer
Because the shared parent folder of both inputs is the project root, the file is emitted to dist/src/index.js. Setting "rootDir": "src" makes the output dist/index.js, but the compiler will then report an error for tests/index.test.ts, since it is outside rootDir. To fix that, compile tests with a separate config, or use noEmit for the test check.
Exercise
A CI job runs a bundler to build the app, and you want it to fail on type errors without writing extra files. Which command or option do you use, and why is noEmitOnError not the right tool here?
Show answer
Run npx tsc --noEmit (or set "noEmit": true). It performs the whole type check and writes nothing. noEmitOnError only controls whether tsc writes files when errors exist, so it does not help when the bundler, not tsc, produces the output.