Skip to content
Latchkey

TS18049: Value is possibly null or undefined - in CI

A named value typed as both null and undefined was used without narrowing either case.

What this error means

Type-checking fails with TS18049 at access on a value typed T | null | undefined.

tsc
src/node.ts(11,5): error TS18049: 'parent' is possibly 'null' or 'undefined'.

Common causes

How to fix it

Truthy-guard both cases

  1. A truthy check rules out both null and undefined for objects
ts
if (parent) parent.append(child)

Default with nullish coalescing

  1. Supply a fallback so downstream code sees a defined value
ts
const p = parent ?? document.body

How to prevent it

  • Model nullability explicitly and narrow once at the boundary rather than repeatedly asserting.

Frequently asked questions

What causes "TS18049 null or undefined"?
Type-checking fails with TS18049 at access on a value typed T | null | undefined.
How do I fix TS18049 null or undefined?
Truthy-guard both cases

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card