TypeScript error TS18048 is reported when a value that might be undefined is accessed without a null check. This is similar to the 'object is possibly null' error but specifically for optional properties and values typed as T | undefined.
Why it happens
- •Accessing a property on an optional field without a guard.
- •Using the result of Array.find() which can return undefined.
- •A function return type includes undefined but the caller doesn't check.
- •Map.get() returns T | undefined and the value is used directly.
Examples
Code that triggers the error
interface User {
name: string;
address?: {
city: string;
};
}
function getCity(user: User): string {
return user.address.city; // address may be undefined
}Error output
error TS18048: 'user.address' is possibly 'undefined'.
How to fix it
Use optional chaining (?.) for safe property access. Add an explicit if check before using the value. Return T | undefined from your function and let callers handle the undefined case. Use nullish coalescing (??) to provide a default value.
Fixed code
function getCity(user: User): string | undefined {
return user.address?.city;
}Related errors
Ready to practice your English at work?
Lingua-e has interactive exercises built around real developer conversations: standups, code reviews, retrospectives, and more. Practice until it comes naturally.
Try Lingua-e for free