{"record":{"id":"7381f9f3963a5e62","repo":"microsoft/TypeScript","slug":"the-generated-content-was-undefined-return-nul","errorCode":null,"errorMessage":"The generated content was \"undefined\". Return \"null\" if no baselining is required.\"","messagePattern":"The generated content was \"undefined\"\\. Return \"null\" if no baselining is required\\.\"","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/harness/harnessIO.ts","lineNumber":1506,"sourceCode":"            else {\r\n                if (!IO.fileExists(expected)) {\r\n                    throw new Error(`New baseline created at ${IO.joinPath(\"tests\", \"baselines\", \"local\", relativeFileName)}`);\r\n                }\r\n                else {\r\n                    throw new Error(errorMessage);\r\n                }\r\n            }\r\n        }\r\n    }\r\n\r\n    function getBaselineFileChangedErrorMessage(relativeFileName: string): string {\r\n        return `The baseline file ${relativeFileName} has changed. (Run \"hereby baseline-accept\" if the new baseline is correct.)`;\r\n    }\r\n\r\n    export function runBaseline(relativeFileName: string, actual: string | null, opts?: BaselineOptions): void { // eslint-disable-line no-restricted-syntax\r\n        const actualFileName = localPath(relativeFileName, opts && opts.Baselinefolder, opts && opts.Subfolder);\r\n        if (actual === undefined) {\r\n            throw new Error('The generated content was \"undefined\". Return \"null\" if no baselining is required.\"');\r\n        }\r\n        const comparison = compareToBaseline(actual, relativeFileName, opts);\r\n        writeComparison(comparison.expected, comparison.actual, relativeFileName, actualFileName, opts);\r\n    }\r\n\r\n    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\r\n        const gen = generateContent();\r\n        const writtenFiles = new Map<string, true>();\r\n        const errors: Error[] = [];\r\n\r\n        // eslint-disable-next-line no-restricted-syntax\r\n        if (gen !== null) {\r\n            for (const value of gen) {\r\n                const [name, content, count] = value as [string, string, number | undefined];\r\n                if (count === 0) continue; // Allow error reporter to skip writing files without errors\r\n                const relativeFileName = relativeFileBase + \"/\" + name + extension;\r\n                const actualFileName = localPath(relativeFileName, opts && opts.Baselinefolder, opts && opts.Subfolder);\r\n                const comparison = compareToBaseline(content, relativeFileName, opts);\r","sourceCodeStart":1488,"sourceCodeEnd":1524,"githubUrl":"https://github.com/microsoft/TypeScript/blob/b465fdbfe175304d9b977da137b2c178ae1091d3/src/harness/harnessIO.ts#L1488-L1524","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Change the generator to `return null;` on the no-content path instead of returning undefined.","Tighten the generator's TypeScript return type to `string | null` so the compiler flags the undefined path at build time.","Coalesce at the call site: `runBaseline(name, content ?? null, opts)`."],"exampleFix":"// before — returns undefined when no content\nfunction gen(): string | null {\n  if (!hasOutput) return;\n  return buildOutput();\n}\n\n// after — explicit null\nfunction gen(): string | null {\n  if (!hasOutput) return null;\n  return buildOutput();\n}","handlingStrategy":"type-guard","validationCode":"// Coalesce undefined to null before calling runBaseline.\nconst safe: string | null = generatedContent ?? null;\nrunBaseline(relativeFileName, safe, opts);","typeGuard":"function isBaselineContent(value: unknown): value is string | null {\n  return value === null || typeof value === 'string';\n}\n\n// usage:\nif (!isBaselineContent(generatedContent)) {\n  throw new TypeError('Generator must return string | null, got ' + typeof generatedContent);\n}","tryCatchPattern":null,"preventionTips":["Type generator returns as `string | null` so the compiler rejects implicit undefined.","Always `return null;` explicitly on the no-content branch.","Coalesce at the call site with `content ?? null`."],"tags":["test-harness","baseline","null-undefined","api-contract"],"backgroundTag":null,"analyzedSha":"b465fdbfe175304d9b977da137b2c178ae1091d3","analyzedAt":"2026-08-12T05:38:42.698Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}