Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Error Codes Worth Knowing

TypeScript errors TS2531 and TS18047: Object is possibly null

Reference for TypeScript errors TS2531 and TS18047 (possibly null): why strictNullChecks reports them and how to fix them with checks, defaults and optional chaining.

With strictNullChecks on, null is its own type and is not part of string, number or object types. When an expression's type includes null and you use it in a way that would crash on null (reading a property, calling a method, indexing), the compiler reports that it is possibly null. TS18047 is the current message and names the variable: 'el' is possibly 'null'. TS2531, Object is possibly 'null', is the older wording used when the expression has no simple name.

Typical sources of null are document.querySelector, Map.prototype.get (which gives undefined, see TS2532), RegExp.exec, JSON parsing and your own T | null fields.

TypeScript
const found = "abc".match(/z/);
found.length; // 'found' is possibly 'null'

Try it

TypeScript Using a value that may be null

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript Handling null explicitly

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

Technique Use when
if (x !== null) { ... } You want to run a block only with a real value.
Early return / throw A missing value is an error for this function.
x?.prop You want undefined instead of a crash; the result type includes undefined.
x ?? fallback You have a sensible default.
x! (non-null assertion) You are certain, and cannot express it in types. It is not checked at run time.
Change the declared type The value can never really be null after initialization.

Edge cases: narrowing does not survive across function boundaries or mutations of properties; copy the value into a const first. For querySelector, pass a type argument or narrow with instanceof to get the element type you need.

Usage notes

  • The code fix is rarely !. A non-null assertion silences the compiler but the program still throws TypeError: Cannot read properties of null when you are wrong.
  • Prefer narrowing close to use. if (el) inside the same function keeps the type precise; checks done in another function do not carry over unless it is a type guard.
  • Closures reset narrowing for let variables that might be reassigned; use const.
  • null vs undefined: == null checks both at once and is the one common use of loose equality.
  • Turning off strictNullChecks removes the error everywhere but also removes the safety; do it only during a migration.
  • Related messages: the same logic with undefined is TS2532/TS18048; with unknown it is TS18046 (TS2571).