microsoft/TypeScript · error · Error

The generated content was "undefined". Return "null" if no b

Error message

The generated content was "undefined". Return "null" if no baselining is required."

What it means

Thrown by runBaseline (harnessIO.ts:1506) when the caller passes `undefined` (not `null`) as the actual content. The harness deliberately distinguishes `null` (= no baseline to write, legitimate skip) from `undefined` (= a generator bug that returned no value) and rejects the latter.

Source

Thrown at src/harness/harnessIO.ts:1506

            else {
                if (!IO.fileExists(expected)) {
                    throw new Error(`New baseline created at ${IO.joinPath("tests", "baselines", "local", relativeFileName)}`);
                }
                else {
                    throw new Error(errorMessage);
                }
            }
        }
    }

    function getBaselineFileChangedErrorMessage(relativeFileName: string): string {
        return `The baseline file ${relativeFileName} has changed. (Run "hereby baseline-accept" if the new baseline is correct.)`;
    }

    export function runBaseline(relativeFileName: string, actual: string | null, opts?: BaselineOptions): void { // eslint-disable-line no-restricted-syntax
        const actualFileName = localPath(relativeFileName, opts && opts.Baselinefolder, opts && opts.Subfolder);
        if (actual === undefined) {
            throw new Error('The generated content was "undefined". Return "null" if no baselining is required."');
        }
        const comparison = compareToBaseline(actual, relativeFileName, opts);
        writeComparison(comparison.expected, comparison.actual, relativeFileName, actualFileName, opts);
    }

    export function runMultifileBaseline(relativeFileBase: string, extension: string, generateContent: () => IterableIterator<[string, string, number]> | IterableIterator<[string, string]> | null, opts?: BaselineOptions, referencedExtensions?: string[]): void { // eslint-disable-line no-restricted-syntax
        const gen = generateContent();
        const writtenFiles = new Map<string, true>();
        const errors: Error[] = [];

        // eslint-disable-next-line no-restricted-syntax
        if (gen !== null) {
            for (const value of gen) {
                const [name, content, count] = value as [string, string, number | undefined];
                if (count === 0) continue; // Allow error reporter to skip writing files without errors
                const relativeFileName = relativeFileBase + "/" + name + extension;
                const actualFileName = localPath(relativeFileName, opts && opts.Baselinefolder, opts && opts.Subfolder);
                const comparison = compareToBaseline(content, relativeFileName, opts);

View on GitHub (pinned to b465fdbfe1)

Solutions

  1. Change the generator to `return null;` on the no-content path instead of returning undefined.
  2. Tighten the generator's TypeScript return type to `string | null` so the compiler flags the undefined path at build time.
  3. Coalesce at the call site: `runBaseline(name, content ?? null, opts)`.

Example fix

// before — returns undefined when no content
function gen(): string | null {
  if (!hasOutput) return;
  return buildOutput();
}

// after — explicit null
function gen(): string | null {
  if (!hasOutput) return null;
  return buildOutput();
}
Defensive patterns

Strategy: type-guard

Validate before calling

// Coalesce undefined to null before calling runBaseline.
const safe: string | null = generatedContent ?? null;
runBaseline(relativeFileName, safe, opts);

Type guard

function isBaselineContent(value: unknown): value is string | null {
  return value === null || typeof value === 'string';
}

// usage:
if (!isBaselineContent(generatedContent)) {
  throw new TypeError('Generator must return string | null, got ' + typeof generatedContent);
}

Prevention

When it happens

Trigger: A baseline generator function returns `undefined` — e.g. `return;`, `return someVar;` where someVar is undefined, or an arrow whose branch yields nothing — and that value reaches runBaseline. The type signature permits `string | null` but JavaScript lets `undefined` through at runtime.

Common situations: Refactoring a generator to conditionally return without a value; loosening a function's return type; a code path that forgets to return null explicitly; migrating from a default-null to an explicit-return pattern.

Related errors


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