Sub topic 11
Emitting declarations without type checking
This chapter assumes you have read the earlier chapters on the tsc pipeline (parse, bind, check, emit), isolatedDeclarations, and tsc --build. You should be comfortable editing tsconfig.json and reading tsc output.
Why split checking from emitting
A normal tsc run does two jobs. It checks types and reports errors, and it writes output files such as .js and .d.ts. Most of the time goes to the check phase. When you need .d.ts files so that another package can start building, you usually don't need the verdict on your own code yet. You only need the declarations.
TypeScript 5.5 added the --noCheck flag for this case. It tells the compiler to skip type checking and go straight to emit. You then run a separate checking pass at the same time as, or after, the emit pass.
The command
The spine's example shows the core command:
tsc --noCheck --declaration --emitDeclarationOnly --outDir distEach flag does one job:
--noCheckskips type checking, so no semantic errors are reported.--declarationasks for.d.tsoutput.--emitDeclarationOnlysuppresses the.jsfiles, so only declarations are written.--outDir distsets where the files go.
The check half of the split is the ordinary command with emit turned off:
tsc --noEmitRun the two commands from the same tsconfig.json. If they use different settings, the declarations you ship can describe a different program from the one you checked.
You can also set the option in the file instead of on the command line:
{
"compilerOptions": {
"declaration": true,
"emitDeclarationOnly": true,
"noCheck": true,
"outDir": "dist"
},
"include": ["src"]
}A tsconfig like this is useful as a second file, for example tsconfig.dts.json, that extends your main one. Run it with tsc -p tsconfig.dts.json. Keep the main tsconfig.json free of noCheck, so a plain tsc still checks everything.
What `--noCheck` does and does not skip
--noCheck turns off error reporting. It does not turn off the type machinery that declaration emit depends on.
When a function has no return type annotation, the declaration writer must know the inferred return type to write it into the .d.ts. To learn it, the compiler resolves types on demand, even under --noCheck. This is the cost the chapter on declaration emit described. Skipping the error pass removes much of the work, but inference-heavy code can still be slow to emit.
isolatedDeclarations removes that remaining cost. When every exported declaration carries an explicit annotation, the declaration writer copies what you wrote and does no inference. The two features complement each other:
| Setup | Checking cost | Inference during emit | Declarations match full tsc output |
|---|---|---|---|
| Plain `tsc` | Full | Yes | Yes |
| `--noCheck` only | Skipped | Yes, on demand | Yes, for programs that type-check |
| `--noCheck` with `isolatedDeclarations` | Skipped | None needed | Yes |
| Third-party emitter with `isolatedDeclarations` | Not applicable | None needed | Yes |
The last row covers the tools in the next chapter. They never load the type checker at all.
Edge cases and failure modes
Errors no longer stop the emit
Without --noCheck, tsc reports type errors but still writes output unless noEmitOnError is set. With --noCheck, there are no errors to react to, so the output is always written. A broken program produces declarations without any warning. The declarations may contain any where inference failed, or types that reflect the broken code.
For this reason --noCheck is only safe as half of a pair. The check job is what decides whether the build is acceptable. Never publish packages from a pipeline that ran only the --noCheck step.
Wrong output from code that does not type-check
Take this file:
export function total(items: number[]) {
return items.reduce((sum, n) => sum + n, "0");
}The reduce call has a type error. Under --noCheck, the emitted declaration is whatever the compiler infers for the broken expression, which may not be what you meant. The check job reports the error, and you fix it. The point is that the .d.ts from the fast step is only trustworthy once the check step has passed on the same sources.
`noCheck` and `noEmitOnError` conflict in spirit
noEmitOnError says "do not write files if there are errors". noCheck says "do not look for errors". Combining them gives you nothing useful, because the first option never sees an error. If you want the safety of noEmitOnError, keep it on the check-and-emit path and leave it off the fast declaration path.
Settings must match
The fast pass and the check pass should share one tsconfig base. Typical mismatches include different strict settings, a different lib, and a different include. Strictness changes inferred types, such as string | undefined versus string, and those differences appear in the .d.ts.
Version requirements
--noCheck needs TypeScript 5.5 or later. On older versions the flag is an unknown option and tsc exits with an error. Use of --noCheck together with --build depends on your TypeScript version, so check the release notes for the version you pin before relying on it.
A worked pipeline
Here is a small package.json that wires up both passes. The typecheck and build:dts scripts can run at the same time.
{
"name": "@example/core",
"private": true,
"scripts": {
"build:dts": "tsc --noCheck --declaration --emitDeclarationOnly --outDir dist",
"typecheck": "tsc --noEmit",
"build": "npm run build:dts & npm run typecheck & wait"
}
}The build script uses shell job control, so it works in bash and zsh but not in Windows cmd. Note one weakness. With & and wait, the exit status of wait with no arguments is zero, so a failing job does not fail the script. A safer form waits on each job:
#!/usr/bin/env bash
set -u
npm run build:dts &
dts_pid=$!
npm run typecheck &
check_pid=$!
wait "$dts_pid"
dts_status=$?
wait "$check_pid"
check_status=$?
if [ "$dts_status" -ne 0 ] || [ "$check_status" -ne 0 ]; then
echo "build failed: dts=$dts_status typecheck=$check_status" >&2
exit 1
fiSave this as build.sh and run it with bash build.sh. It exits with an error if either pass fails.
In a CI system, the same idea is two jobs in one workflow. This GitHub Actions example shows a job that emits declarations and a job that checks:
name: build
on: [push]
jobs:
declarations:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
- run: npm ci
- run: npm run build:dts
- uses: actions/upload-artifact@v4
with:
name: dts
path: dist
typecheck:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
- run: npm ci
- run: npm run typecheckBoth jobs start together. Other jobs that need the declarations can depend on declarations alone and download the artifact, while typecheck finishes on its own schedule. Make the pull request depend on both jobs, for example with branch protection that requires both to pass.
Decision criteria
Use the split when all of these are true:
- Downstream packages wait on your
.d.tsfiles, so declaration emit is on the critical path. - A full
tscrun takes long enough that you notice it, as measured earlier with--extendedDiagnostics. - You can run the check job at the same time, so the total time shrinks instead of the work simply being done twice.
Skip the split when your project is small, because two processes each parse and bind the whole program and the overhead can outweigh the gain. Also skip it when you cannot guarantee the check step runs, for example in a local script that nobody reviews.
Combine the split with isolatedDeclarations when you can. Without it, the fast pass still pays for inference. With it, the pass gets cheaper, and you also gain the option of using a parallel emitter, covered next.
Common errors
error TS5023: Unknown compiler option '--noCheck'. Your installed TypeScript is older than 5.5. Runnpx tsc --versionand upgrade.- Declarations contain
anyin places you did not expect. The program has type errors, and the fast pass emitted whatever it could infer. Run the check job and fix the errors first. - The check job passes but consumers see different types. The two passes used different tsconfig settings. Move shared options into one base file and extend it.
- The
buildscript exits with zero although a job failed. You used&with a barewait. Wait on each process ID and check its exit status, as inbuild.shabove.
Exercise
Your CI has one job that runs tsc --noCheck --declaration --emitDeclarationOnly --outDir dist and then publishes the package. The job is green. Why is this unsafe, and what is the smallest change that fixes it?
Show answer
The job never type-checks, so a program with type errors still produces a green job and published declarations that may be wrong (for example, containing any). The smallest fix is to add a second step or job that runs tsc --noEmit with the same tsconfig base, and make the publish step depend on both passing.
Exercise
A project already has isolatedDeclarations enabled and every export annotated. You run the --noCheck declaration pass and it is still slow. Name one thing to measure and what a result would tell you.
Show answer
Run it with --extendedDiagnostics and read the parse and bind times. Because checking is skipped and no inference is needed, a large remaining time points to program size (too many files or large dependency typings pulled in through include or imports), not type inference. Narrowing include, setting skipLibCheck-style options for the check pass, or splitting the code into composite projects would be the next steps.