Home/TypeScript Errors/TS2531
Object is possibly 'null'
TypeScript TS2531 fires when you try to access a property or method on a value that could be null. With strictNullChecks enabled, TypeScript forces you to handle the null case before accessing the value.
Why This Happens
The variable's type includes null (e.g., Element | null, string | null), and you're accessing it without first checking that it's not null. Common with DOM APIs like getElementById, querySelector, and with nullable database results.
Code That Triggers TS2531
// TS2531 examples
const el = document.getElementById("app");
el.addEventListener("click", handler);
// Error: Object is possibly 'null'
// getElementById returns HTMLElement | null
const user = users.find(u => u.id === id);
console.log(user.name);
// Error: Object is possibly 'null'
// Array.find returns T | undefinedHow to Fix TS2531
Option 1: Null check with if statement
const el = document.getElementById("app");
if (el) {
el.addEventListener("click", handler); // OK
}Option 2: Non-null assertion operator (!) — use carefully
const el = document.getElementById("app")!;
// Tells TypeScript: "trust me, this is never null"
// Will throw at runtime if it actually is nullOption 3: Optional chaining (?.) to skip null safely
const el = document.getElementById("app");
el?.addEventListener("click", handler); // silently skips if nullOption 4: Nullish coalescing (??) to provide a fallback
const user = users.find(u => u.id === id) ?? defaultUser;
console.log(user.name); // safe — always has a valueFrequently Asked Questions — TS2531
What does TS2531 mean?
TS2531 means you're using a value that TypeScript knows might be null without checking first. It's a compile-time warning that protects you from null reference errors at runtime.
How is TS2531 different from TS2532?
TS2531 is 'possibly null', TS2532 is 'possibly undefined'. They're the same concept for different nullish values. Both require you to handle the nullish case before accessing properties.
Should I use ! (non-null assertion) to fix TS2531?
Only when you're absolutely certain the value can't be null at that point. The ! operator tells TypeScript to trust you — if you're wrong, you get a runtime TypeError instead of a compile error.
TS2531 on document.getElementById — how to fix?
Use an if check: if (el) { el.addEventListener(...) }, or use optional chaining: el?.addEventListener(...). getElementById returns HTMLElement | null because the element may not exist in the DOM.
Why does TS2531 only appear with strict mode?
TS2531 requires strictNullChecks (part of strict: true). Without it, null is assignable to every type, so TypeScript doesn't distinguish nullable from non-nullable values.
Convert JavaScript to TypeScript automatically
Paste your JS code and get type-annotated TypeScript — including fixes for common type errors — in seconds.
Try the converter →Other TypeScript Errors
From the blog
View all →
May 13, 2026
Convert XML to JSON Online: Complete Guide for Developers (2026)

May 11, 2026
Convert CSV to JSON Online Free (Best Developer Guide 2026)

May 6, 2026
Convert YAML to JSON Online Free (2026 Developer Guide)

Apr 30, 2026
Convert JSON to Zod Schema Online (2026) – Free Tool & Complete Guide

Apr 30, 2026
Top 50 JavaScript to TypeScript Converters (2026) – Free, AI & Online Tools