TypeScript Compiler Diagnostics
TS18048: Value is possibly undefined
An optional property may be absent, so accessing a string method directly is unsafe. Nullish coalescing supplies a fallback only for null or undefined; it preserves an intentionally empty string. Choose the fallback according to the domain rather than disabling strictNullChecks. Baseline: TypeScript 5.8.3 with the shown configuration. Reproduce in one directory and create the corrected project in another. The diagnostic can have other causes; inspect its location and message before choosing a correction. Both projects keep strict checking enabled.
Formal Syntax & Structure
typescript syntaxtypescript
TS18048 (compiler diagnostic code, not TypeScript syntax)Working Example
Example Snippettypescript
// INTENTIONALLY INVALID — reproduce// File: tsconfig.json (omit this label){ "compilerOptions": { "strict": true, "target": "ES2022", "module": "CommonJS", "types": [], "lib": [ "ES2022", "DOM" ], "noEmitOnError": true, "outDir": "dist" }, "files": [ "main.ts" ]}
// File: main.ts (omit this label)function label(item: { name?: string }) { return item.name.toUpperCase(); }
// Run: tsc -p . --pretty false// Expected: TS18048; no output emitted.
// CORRECTED PROJECT — separate directory// File: tsconfig.json (omit this label){ "compilerOptions": { "strict": true, "target": "ES2022", "module": "CommonJS", "types": [], "lib": [ "ES2022", "DOM" ], "noEmitOnError": true, "outDir": "dist" }, "files": [ "main.ts" ]}
// File: main.ts (omit this label)function label(item: { name?: string }) { return (item.name ?? "unnamed").toUpperCase(); }console.log(label({name:"Ada"}), label({}), JSON.stringify(label({name:""})));
// Run: tsc -p . --pretty false// Expected: no diagnostics.// Run: node dist/main.js// Expected stdout: ADA UNNAMED ""