Error Encyclopedia
TS18048

TS18048: 'x' is possibly 'undefined'

Fix TypeScript error TS18048 'is possibly undefined'. Learn to use optional chaining, nullish coalescing, and strict null checks correctly.

What Does This Error Mean?

TypeScript error TS18048 'is possibly undefined' occurs when strictNullChecks is enabled and you try to access a property or call a method on a value that could be undefined or null. TypeScript forces you to handle the null/undefined case before accessing the value.

Common Causes

1

Accessing a property on a value that could be undefined

2

Calling a method on an optional function parameter

3

Array access returning an element that could be undefined (out of bounds)

4

Optional chaining used but result still possibly undefined

5

Function return type includes undefined but caller assumes it is always defined

6

Object property access on a nullable type without narrowing

How to Fix It

Use optional chaining (?.)

Access properties safely on potentially null/undefined values.

interface User {
  name: string
  address?: {
    street: string
    city: string
  }
}

// ❌ address is possibly undefined
console.log(user.address.street)

// ✅ Optional chaining
console.log(user.address?.street ?? "No address")

// ✅ Chained optional access
const city = user?.address?.city ?? "Unknown"

Use nullish coalescing (??)

Provide default values for possibly null/undefined expressions.

function greet(name?: string) {
  // ❌ name is possibly undefined
  console.log(`Hello, ${name.toUpperCase()}`)

  // ✅ Default value
  const safeName = name ?? "Guest"
  console.log(`Hello, ${safeName.toUpperCase()}`)
}

Use type guards to narrow the type

Check for null/undefined before accessing the value.

function processUser(user: User | undefined) {
  // ❌ user is possibly undefined
  console.log(user.name)

  // ✅ Type guard
  if (user) {
    console.log(user.name)  // TypeScript knows user is defined here
  }

  // ✅ Or throw if required
  if (!user) throw new Error("User required")
  console.log(user.name)  // Defined after the throw
}

Use the non-null assertion operator (!)

Use ! to assert a value is not null/undefined (use sparingly).

// Only use when you are absolutely sure the value exists
function findUser(id: number): User | undefined {
  return users.find(u => u.id === id)
}

// We know this user exists (just created it)
const user = findUser(1)!
console.log(user.name)  // ✅ Non-null assertion

Related Errors

Other common errors in this category:

Frequently Asked Questions

What does strictNullChecks do?

When enabled (recommended), strictNullChecks makes TypeScript distinguish between `string | undefined` and `string`. You must handle undefined/null before using a value. It catches a whole class of runtime errors at compile time.

Should I use non-null assertion (!) or optional chaining (?.)?

Prefer optional chaining (?.) and type guards over non-null assertions. The ! operator suppresses TypeScript's null checking — if you are wrong, you get a runtime error. Only use it when you have a guarantee the value exists (e.g., a ref that was just assigned).