TS18047
TypeScriptERRORNotableTypeHIGH confidence

'X' is possibly 'null'.

Production Risk

Build will fail; resolve before shipping.

What this means

A TypeScript diagnostic (TS18047): 'X' is possibly 'null'.. This diagnostic is emitted by the TypeScript compiler when 'X' is possibly 'null'..

Why it happens
  1. 1The value may be null or undefined at this point in the code
  2. 2Strict null checks are enabled and the type includes null or undefined
How to reproduce

TypeScript compiler reports TS18047 during type checking.

trigger — this will error
trigger — this will error
// Triggers TS18047
// 'X' is possibly 'null'.

expected output

error TS18047: 'X' is possibly 'null'.

Fix

Add null check before accessing

WHEN When the value might be null or undefined

Add null check before accessing
// Use optional chaining
const result = obj?.property;
// Or null assertion (only if certain it is non-null)
const result = obj!.property;
// Or narrow with a type guard
if (obj !== null && obj !== undefined) {
  const result = obj.property;
}

Why this works

Optional chaining (?.) returns undefined instead of throwing; nullish coalescing (??) provides fallback values.

What not to do

Use non-null assertion (!) without being certain the value is non-null

Non-null assertions bypass type safety; a null value will still cause a runtime error.

Sources
Official documentation ↗

TypeScript Compiler Diagnostics

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All TypeScript errors