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

**Code:** `TS2531: Object is possibly 'null'` · **Category:** TypeScript · **Seen on:** TypeScript

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

## 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?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/typescript/ts2531-object-is-possibly-null)
