TS2531: Object is possibly 'null' TypeScript Origin: On your device OSI layer 7 · Application Last updated 5 Nov 2026

TypeScript: error TS2531: Object is possibly 'null'.

You used a value that can be null without checking it first (strictNullChecks).

Exact message you may see error TS2531: Object is possibly 'null'.TS2531: Object is possibly 'null'.Object is possibly 'null'. ts(2531)error TS2531: Object is possibly 'null'
Seen on: TypeScript

Meaning

Narrow before use: if (el) { el.value }, optional chaining el?.value, nullish defaults el ?? fallback, or a non-null assertion el! only when you're certain. Typical with document.getElementById, querySelector, ref.current, Array.find (undefined).

Common causes

  • DOM queries returning T | null
  • React useRef<T>(null) current
  • Map/Array lookups
  • Optional API data
  • Variables assigned later

⚡ Quick fix

  1. const el = document.getElementById("x"); if (!el) return;
  2. Optional chaining el?.value
  3. Non-null assertion el! only when guaranteed

Detailed fix by platform

TypeScript

  1. const input = document.querySelector<HTMLInputElement>("#name"); if (input) input.value = "A";

How to diagnose

  1. Value — Can be null?
  2. Narrow — Checked?
  3. Assertion — Safe?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: On your device. Errors that are often confused with or caused by this one (click to open):

Cannot find module './App.vue' or its corresponding type declarations — TypeScript: error TS2307: Cannot find module './App.vue' or its corresponding type declarations. (Vue)Cannot find module '.…TS1005: ';' expected / ',' expected (syntax error) — TypeScript: error TS1005: ';' expected. (TypeScript)TS1005: ';' expected …TS1128: Declaration or statement expected — TypeScript: error TS1128: Declaration or statement expected. (TypeScript)TS1128: Declaration o…TS1192: Module has no default export — TypeScript: error TS1192: Module '"./utils"' has no default export. (TypeScript)TS1192: Module has no…TS1208: File cannot be compiled under '--isolatedModules' because it is considered a global script file — TypeScript: error TS1208: 'index.ts' cannot be compiled under '--isolatedModules' because it is considered a global script file. Add an import, export, or an empty 'export {}' statement (TypeScript)TS1208: File cannot b…TS18048: 'x' is possibly 'undefined' — TypeScript: error TS18048: 'user' is possibly 'undefined'. (TypeScript)TS18048: 'x' is possi…TS2322 — TS2322: Type 'X' is not assignable to type 'Y' (TypeScript)TS2322TS2532 / TS18048 — TS18048: 'x' is possibly 'undefined'. (TS2532: Object is possibly 'undefined'.) (Node.js)TS2532 / TS18048TS2531: O…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.