microsoft/TypeScript · error · Error

Diagnostic code ${code} appears more than once.

Error message

Diagnostic code ${code} appears more than once.

What it means

processDiagnosticMessages reads src/compiler/diagnosticMessages.json and runs checkForUniqueCodes, which flags any numeric `code` that appears in more than one entry. Duplicate codes would corrupt diagnostic lookup and reporting, so code-gen refuses to proceed.

Source

Thrown at scripts/processDiagnosticMessages.mjs:73

    }

    const infoFileOutput = buildInfoFileOutput(diagnosticMessages, inputFilePath);
    checkForUniqueCodes(diagnosticMessages);
    await writeFile("diagnosticInformationMap.generated.ts", infoFileOutput);

    const messageOutput = buildDiagnosticMessageOutput(diagnosticMessages);
    await writeFile("diagnosticMessages.generated.json", messageOutput);
}

/**
 * @param {InputDiagnosticMessageTable} diagnosticTable
 */
function checkForUniqueCodes(diagnosticTable) {
    /** @type {Record<number, true | undefined>} */
    const allCodes = [];
    diagnosticTable.forEach(({ code }) => {
        if (allCodes[code]) {
            throw new Error(`Diagnostic code ${code} appears more than once.`);
        }
        allCodes[code] = true;
    });
}

/**
 * @param {InputDiagnosticMessageTable} messageTable
 * @param {string} inputFilePathRel
 * @returns {string}
 */
function buildInfoFileOutput(messageTable, inputFilePathRel) {
    const result = [
        "// <auto-generated />",
        `// generated from '${inputFilePathRel}'`,
        "",
        'import { DiagnosticCategory, DiagnosticMessage } from "./types.js";',
        "",
        "function diag(code: number, category: DiagnosticCategory, key: string, message: string, reportsUnnecessary?: {}, elidedInCompatabilityPyramid?: boolean, reportsDeprecated?: {}): DiagnosticMessage {",

View on GitHub (pinned to b465fdbfe1)

Solutions

  1. Search diagnosticMessages.json for the duplicated code and renumber the new entry.
  2. Pick the next free code in the appropriate range (consult neighboring entries).
  3. Re-run `hereby generate-diagnostics` (or the processDiagnosticMessages task) to confirm.

Example fix

// before
{ "code": 1234, "category": "Error", ... }  // appears twice

// after
// change the new diagnostic to a free code, e.g.
{ "code": 1235, "category": "Error", ... }
Defensive patterns

Strategy: validation

Validate before calling

const codes = new Set();
for (const { code } of diagnosticMessages) {
  if (codes.has(code)) throw new Error(`Duplicate diagnostic code ${code}`);
  codes.add(code);
}

Prevention

When it happens

Trigger: Adding a new diagnostic whose `code` is already used by another entry in diagnosticMessages.json.

Common situations: A contributor picks an already-allocated TS diagnostic code; a rebase brings in two diagnostics with the same code.

Related errors


AI-assisted analysis of microsoft/TypeScript@b465fdbfe1 (2026-08-12). Data as JSON: /api/errors/27c53ec63e629990. Report an issue: GitHub.