Sub topic 11
Emitting Declarations Without Type Checking
The two jobs in a TypeScript build
This chapter assumes that you can run tsc, edit tsconfig.json, and understand basic .d.ts declaration files. It also assumes that your project already uses, or is ready to consider, isolatedDeclarations.
A TypeScript build commonly performs two separate jobs:
- Type checking finds invalid assignments, missing properties, and other type errors.
- Declaration emit writes
.d.tsfiles that describe the public types of your code.
These jobs often run together. For a large project, that can make declaration generation wait for a complete type-checking pass.
TypeScript 5.5 introduced the --noCheck option. It tells the compiler to skip the normal full type-checking pass during emit. You can then run type checking separately.
flowchart TD
A["Source files"] --> B["Fast emit with --noCheck"]
A --> C["Full check with --noEmit"]
B --> D["JavaScript and .d.ts files"]
C --> E["Type errors"]caption: Separating declaration emit from type checking alt: A source tree splits into a fast emit process that creates JavaScript and declaration files, and a separate full check process that reports type errors.
A split build
Suppose tsconfig.json contains declaration settings:
{
"compilerOptions": {
"strict": true,
"declaration": true,
"emitDeclarationOnly": true,
"outDir": "dist"
},
"include": ["src"]
}Run the two jobs independently:
# Produce .d.ts files without the full checking pass
tsc --noCheck --declaration --emitDeclarationOnly --outDir dist
# Check every source file without writing output
tsc --noEmitThe first command is useful when another process needs declarations quickly. The second command remains the correctness gate. A fast declaration build must not replace type checking.
In a package that also emits JavaScript, use separate output directories or separate configurations. Otherwise, two concurrent compiler processes might write the same files.
| Job | Important options | Main result |
|---|---|---|
| Declaration emit | `--noCheck --declaration` | `.d.ts` files |
| Type checking | `--noEmit` | Diagnostics |
| JavaScript emit | `--noCheck` or a normal build | `.js` files |
Why isolatedDeclarations improves this approach
Without isolatedDeclarations, the compiler may still need to inspect other files to infer the public type of an exported value. For example, an exported function without a return annotation can require type analysis before its declaration is written.
isolatedDeclarations requires exported declarations to contain enough local information for a declaration file to be generated one file at a time. This makes parallel declaration emitters possible and makes --noCheck more predictable.
// Good for isolated declaration emit
export function add(a: number, b: number): number {
return a + b;
}
export interface Config {
retries: number;
}
declare function loadConfig(): Config;
export const config: Config = loadConfig();The explicit return type on add and the explicit variable type on config describe the public API without requiring inference from unrelated files.
Trade-offs and failure modes
--noCheck improves latency, but it does not make incorrect code correct. A declaration can be emitted while the source still contains type errors. Run the checking command before publishing a package or merging a change.
A project can also fail during declaration emit if exported values lack enough information and TypeScript must resolve their types. Enabling isolatedDeclarations exposes these cases as annotation errors. Fix those errors instead of hiding them with weaker compiler settings.
Some third-party declaration emitters use the same local-information rule and can generate files in parallel. They can be faster than tsc for very large projects, but they may not reproduce every TypeScript compiler behavior. Compare their output with tsc before adopting one.
> callout tip > Use --noCheck to shorten feedback loops, not to remove the type-checking stage. Keep a separate tsc --noEmit command in local development and continuous integration.
A practical script layout
{
"scripts": {
"check": "tsc --noEmit",
"emit:types": "tsc --noCheck --declaration --emitDeclarationOnly --outDir dist",
"build": "npm run check && npm run emit:types"
}
}For local work, you can run check and emit:types concurrently when they write to separate outputs. In continuous integration, run both and fail the job if either command fails.
Exercise
Why should a project keep tsc --noEmit after adding tsc --noCheck?
Show answer
--noCheck speeds up output generation, but it does not report the full set of type errors. --noEmit remains the correctness check.