microsoft/TypeScript · error · Error
Only declaration files should be generated when emitDeclarat
Error message
Only declaration files should be generated when emitDeclarationOnly:true
What it means
Thrown by prepareDeclarationCompilationContext when options.declaration and options.emitDeclarationOnly are both set and compilation produced no errors, yet the result violates emitDeclarationOnly semantics: either JS files were emitted (result.js.size > 0) or, when noEmit is false, no declaration files were produced (result.dts.size === 0). The invariant is that with emitDeclarationOnly the compiler must emit only .d.ts files.
Source
Thrown at src/harness/harnessIO.ts:403
declOtherFiles: TestFile[];
harnessSettings: TestCaseParser.CompilerSettings & HarnessOptions | undefined;
options: ts.CompilerOptions;
currentDirectory: string;
}
export function prepareDeclarationCompilationContext(
inputFiles: readonly TestFile[],
otherFiles: readonly TestFile[],
result: compiler.CompilationResult,
harnessSettings: TestCaseParser.CompilerSettings & HarnessOptions,
options: ts.CompilerOptions,
// Current directory is needed for rwcRunner to be able to use currentDirectory defined in json file
currentDirectory: string | undefined,
): DeclarationCompilationContext | undefined {
if (options.declaration && result.diagnostics.length === 0) {
if (options.emitDeclarationOnly) {
if (result.js.size > 0 || (result.dts.size === 0 && !options.noEmit)) {
throw new Error("Only declaration files should be generated when emitDeclarationOnly:true");
}
}
else if (result.dts.size !== result.getNumberOfJsFiles(/*includeJson*/ false)) {
throw new Error("There were no errors and declFiles generated did not match number of js files generated");
}
}
const declInputFiles: TestFile[] = [];
const declOtherFiles: TestFile[] = [];
// if the .d.ts is non-empty, confirm it compiles correctly as well
if (options.declaration && result.diagnostics.length === 0 && result.dts.size > 0) {
ts.forEach(inputFiles, file => addDtsFile(file, declInputFiles));
ts.forEach(otherFiles, file => addDtsFile(file, declOtherFiles));
return { declInputFiles, declOtherFiles, harnessSettings, options, currentDirectory: currentDirectory || harnessSettings.currentDirectory };
}
function addDtsFile(file: TestFile, dtsFiles: TestFile[]) {
View on GitHub (pinned to b465fdbfe1)
Solutions
- Verify the compiler actually respects emitDeclarationOnly for the given input — if JS is being emitted, this may be a real compiler bug to file.
- Check that noEmit is not set; if noEmit is intended, no .d.ts will be produced and the second clause triggers.
- Inspect result.js and result.dts contents to see which files were unexpectedly emitted (or missing).
Example fix
// before — test options conflict // @declaration: true // @emitDeclarationOnly: true // @noEmit: true // after — remove noEmit so declarations are emitted // @declaration: true // @emitDeclarationOnly: true
Defensive patterns
Strategy: validation
Validate before calling
// Sanity-check emit invariants before baselining declarations
if (options.declaration && options.emitDeclarationOnly) {
if (options.noEmit) throw new Error("emitDeclarationOnly requires noEmit:false");
if (result.js.size > 0) throw new Error("Compiler emitted JS under emitDeclarationOnly");
} Prevention
- Do not combine emitDeclarationOnly with noEmit.
- When authoring declaration tests, verify the compiler emits only .d.ts files.
When it happens
Trigger: A compiler test sets declaration:true and emitDeclarationOnly:true but the compilation result contains emitted JS, or produces zero .d.ts files while noEmit is false. This indicates the compiler output diverges from the declared intent.
Common situations: A test harness bug, a compiler regression that emits JS despite emitDeclarationOnly, or a test configuration where emitDeclarationOnly was set but noEmit was also unexpectedly true. Also seen when a transform/plugin interferes with emit filtering.
Related errors
- There were no errors and declFiles generated did not match n
- No sourcemap files should be generated if inlineSourceMaps w
- Number of sourcemap files should be same as js files.
- Cannot have undefined value for compiler option '${name}'.
- Unknown value '${value}' for compiler option '${name}'.
AI-assisted analysis of microsoft/TypeScript@b465fdbfe1 (2026-08-12).
Data as JSON: /api/errors/5460f7922332fc64.
Report an issue: GitHub.