Lingua-e
← Errors
JavaScript

TS18048: Possibly Undefined: What It Means and How to Fix It

Error reference

TS18048: Possibly Undefined

Language

JavaScript

Severity

Medium

When it happens

TypeScript error TS18048 is reported when a value that might be undefined is accessed without a null check.

Potential fixes

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.

Deep dive

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.

Potential fixes

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.

Examples

JavaScript

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'.

Fixed code

function getCity(user: User): string | undefined {
  return user.address?.city;
}
Practice in English

How would you explain a TS18048: Possibly Undefined to a fellow dev? Choose the right phrase:

"I hit an error...

For developers

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 →

No credit card needed

Vocabulary
merge conflictnoun

when two branches edit the same lines

Fill the gap

The build failedbecause of a missing dependency.

Multiple choice

What do you say when you're blocked?

AI'm blocked on this ticket.
BI am block on ticket.