Sub topic 10
Controlling What `tsc` Emits
This chapter assumes you know JavaScript functions, modules, npm scripts, and basic terminal commands. You should also know that TypeScript files do not run directly in a normal Node.js JavaScript environment. The TypeScript compiler, tsc, checks your code and can write JavaScript files that a runtime can execute.
Type-checking and emitting are separate jobs
When tsc processes a TypeScript file, it performs two related but separate jobs:
- It checks whether the code follows the declared type rules.
- It emits, or writes, JavaScript and other output files.
Type annotations help during checking, but they do not remain in ordinary JavaScript output.
function greet(name: string): string {
return `Hello, ${name}!`;
}
console.log(greet("Ada"));The emitted JavaScript contains the function and its behavior, but not : string:
function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet("Ada"));The target compiler option controls the JavaScript language level. For example, ES2022 allows modern JavaScript syntax in the output. A lower target can make newer syntax compatible with older runtimes.
Emit JavaScript and source maps
A source map connects emitted JavaScript back to the original TypeScript. Editors and browser developer tools use it to show the TypeScript source while the runtime executes JavaScript.
A project might use these settings:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"rootDir": "src",
"outDir": "dist",
"sourceMap": true,
"strict": true
},
"include": ["src"]
}With src/index.ts as an input, this configuration can produce:
| Input | Output |
|---|---|
| `src/index.ts` | `dist/index.js` and `dist/index.js.map` |
The outDir option keeps generated files separate from source files. The rootDir option tells tsc which directory contains the source tree. Keeping these directories separate prevents generated files from being compiled again accidentally.
Check without writing files
Use noEmit when another tool handles transpilation or bundling, but you still want TypeScript to check the project.
npx tsc --noEmitThis command reads the project configuration, reports type errors, and writes no JavaScript, source maps, or declaration files. It is common in continuous integration, where a successful type check is one required test.
You can also put the setting in tsconfig.json:
{
"compilerOptions": {
"strict": true,
"noEmit": true
},
"include": ["src"]
}A command-line option overrides the corresponding configuration value for that run. Therefore, you can keep noEmit in a checking configuration and use a separate build configuration when you need output.
Decide what happens after errors
By default, tsc can report type errors and still emit JavaScript. That output may be useful for investigation, but it is usually unsafe to publish.
Set noEmitOnError to prevent output when checking fails:
{
"compilerOptions": {
"strict": true,
"outDir": "dist",
"noEmitOnError": true
},
"include": ["src"]
}This option affects emit, not error reporting. The compiler still displays the errors. It only refuses to write output for a failed compilation.
See the output decision
The following model shows the main choices:
Choose noEmit when a bundler or runtime transforms TypeScript. Choose normal emit when tsc owns the build output. Choose noEmitOnError when generated JavaScript must never come from code that failed type checking.
Exercise
A project uses a bundler to transform TypeScript, but its CI job should reject type errors and create no JavaScript. Which command should the job run?
Show answer
Run npx tsc --noEmit. The bundler handles transformation, while tsc performs the type check without emitting files.