Skip to content

no-undefined-check-for-null ​

A value from an API that returns null (localStorage.getItem, querySelector, parentElement, RegExp#exec, ...) compared with undefined.

Why: SAFE-TS code has no null (no-null), so absence is undefined. But the browser and JavaScript's own APIs still return null, and after the move from null to undefined, getItem(key) !== null easily becomes getItem(key) !== undefined, which is always true. TypeScript accepts any comparison with undefined, so nothing else notices. no-null allows comparing with null, so the check can say what it means. Guide: Absence.

❌ Rejected: always true, the key is never "missing"

ts
export function hasDraft(): boolean {
  return localStorage.getItem("draft") !== undefined;
}

export function inForm(input: Element): boolean {
  const form = input.closest("form");
  return form !== undefined;
}
✓ Accepted0 diagnostics
ts
export function hasDraft(): boolean {
  return localStorage.getItem("draft") !== null;
}

export function inForm(input: Element): boolean {
  const form = input.closest("form");
  return form !== null;
}

Or turn the value into an Option where it comes in, and check for undefined from then on:

✓ Accepted0 diagnostics
ts
import type { Option } from "@evobug_com/safe-ts-std/result";

export function draft(): Option<string> {
  return localStorage.getItem("draft") ?? undefined;
}

typeof value === "undefined" is reported too (always false for null), and so is value == undefined: it works, because null == undefined, but it reads as a check for undefined and hides where the null comes from.

With the browser or Node pack, the API's safe version returns an Option, and comparing it with undefined is right:

✓ Accepted0 diagnostics
ts
import { get } from "@evobug_com/safe-ts-feature-browser/fetch";
import { closest } from "@evobug_com/safe-ts-feature-browser/dom";

export function onPage(params: URLSearchParams): boolean {
  return get(params, "page") !== undefined;
}

export function inForm(input: Element): boolean {
  return closest(input, "form") !== undefined;
}

In the browser pack, getItem and key of Storage also throw (a denied or damaged store), so their safe versions return a Result of an Option: unwrap it first, then compare with undefined. The rule offers only Compare with null for them, and unhandled-failure reports the call:

✓ Accepted0 diagnostics
ts
import { getItem } from "@evobug_com/safe-ts-feature-browser/storage";
import { q, type Result } from "@evobug_com/safe-ts-std/result";

export function hasDraft(): Result<boolean, unknown> {
  const draft = q(getItem(localStorage, "draft"));
  return draft !== undefined;
}
ts
import { getItem } from "@evobug_com/safe-ts-feature-browser/storage";
import { Err as __qErr } from "@evobug_com/safe-ts-std/result"; import { q, type Result } from "@evobug_com/safe-ts-std/result";

export function hasDraft(): Result<boolean, unknown> {
  const __q1 = getItem(localStorage, "draft"); if (__q1 instanceof __qErr) return __q1; const draft = __q1;
  return draft !== undefined;
}

Quick fixes: Compare with null, which says what happens to the result: (the same as now) for == undefined, which already passed for null, and (was always false) or (was always true) for === undefined / !== undefined. With a pack that has the API's safe version: Use the safe version, which returns an Option, which rewrites the call (params.get("page") to get(params, "page"), also in the const it was stored in) and adds the import; the comparison with undefined is then right. The help names the safe version too. RegExp#exec, String#match and Object.getPrototypeOf are the language's, not a pack's: for them, compare with null or write ?? undefined.

Which APIs: there is no type checker, so the list is curated from lib.dom and the JavaScript built-ins, by names specific enough to go by:

  • methods: querySelector, getElementById, closest, getAttribute (and getAttributeNS, getAttributeNode), getContext, getSelection, elementFromPoint, getResponseHeader, getNamedItem, RegExp#exec, String#match, Object.getPrototypeOf, prompt(), a few WebGL getters;
  • getItem and key on localStorage, sessionStorage or a value typed Storage; get on url.searchParams, x.headers, or a value typed or made as URLSearchParams, Headers or FormData (a Map's get gives undefined);
  • properties: parentElement, parentNode, the sibling and child properties (nextSibling, firstElementChild, ...), offsetParent, document.activeElement, fullscreenElement, currentScript, shadowRoot, contentWindow, relatedTarget, dataTransfer, textContent, nodeValue, and a few more;
  • a const initialized from any of these.

What the rule can't see: a value that comes through a let, a parameter or a function's return value, and APIs with generic names (event.target, list.item(i), onclick), which your own objects may use with undefined. An optional chain (el?.getAttribute("x")) can be undefined as well, so comparing it with undefined is not reported.

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra