Skip to content

no-null-check-for-undefined ​

A value whose type has no null (an Option, a field declared ?:, a pack's safe version) compared with null, or expected to be null by an assertion. The mirror of no-undefined-check-for-null.

Why: SAFE-TS code has no null (no-null): an absent Option<T> is undefined. After the move from T | null to Option<T>, checks written for null still compile, and no-null allows === null (it is how outside data is tested), but they never match: a loop waiting for next === null never stops, assert.equal(x, null) always fails and expect(x).not.toBeNull() always passes, checking nothing. TypeScript accepts any comparison with null, so nothing else notices. Guide: Absence.

❌ Rejected: the loop never stops, nextDue gives undefined when nothing is due

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

type Invoice = { readonly id: string; readonly paidAt: Option<number> };

function nextDue(invoices: readonly Invoice[]): Option<Invoice> {
  return invoices.find((i) => i.paidAt === undefined);
}

export function countDue(invoices: readonly Invoice[]): number {
  let count = 0;
  let rest = invoices;
  for (;;) {
    const due = nextDue(rest);
    if (due === null) break;
    count++;
    rest = rest.filter((i) => i !== due);
  }
  return count;
}
✓ Accepted0 diagnostics
ts
import type { Option } from "@evobug_com/safe-ts-std/result";

type Invoice = { readonly id: string; readonly paidAt: Option<number> };

function nextDue(invoices: readonly Invoice[]): Option<Invoice> {
  return invoices.find((i) => i.paidAt === undefined);
}

export function countDue(invoices: readonly Invoice[]): number {
  let count = 0;
  let rest = invoices;
  for (;;) {
    const due = nextDue(rest);
    if (due === undefined) break;
    count++;
    rest = rest.filter((i) => i !== due);
  }
  return count;
}

In tests: null as the expected value of an assertion, also at one element of an array or object compared as a whole. With vitest or jest, expect(todo.doneAt).toBeNull() always fails the same way, and expect(todo.doneAt).not.toBeNull() always passes: it checks nothing.

❌ Rejected: both always fail

ts
import assert from "node:assert/strict";
import { test } from "node:test";
import type { Option } from "@evobug_com/safe-ts-std/result";

type Todo = { readonly title: string; readonly doneAt: Option<number> };

test("a new todo is open", () => {
  const todo: Todo = { title: "milk", doneAt: undefined };
  assert.equal(todo.doneAt, null);
  assert.deepEqual([todo.title, todo.doneAt], ["milk", null]);
});

✅ Accepted: and expect(todo.doneAt).toBeUndefined(), .toBeDefined()

✓ Accepted0 diagnostics
ts
import assert from "node:assert/strict";
import { test } from "node:test";
import type { Option } from "@evobug_com/safe-ts-std/result";

type Todo = { readonly title: string; readonly doneAt: Option<number> };

test("a new todo is open", () => {
  const todo: Todo = { title: "milk", doneAt: undefined };
  assert.equal(todo.doneAt, undefined);
  assert.deepEqual([todo.title, todo.doneAt], ["milk", undefined]);
});

The value of a pack's safe version that returns an Option is never null either: the safe version has turned the original's null into undefined.

❌ Rejected: get(params, k) gives undefined for a missing key, never null

ts
import { get } from "@evobug_com/safe-ts-feature-browser/fetch";

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

value == null and value != null are reported too: they work, because undefined == null, but they read as a check for null. So is an assertion that compares with == (node:assert's legacy equal and deepEqual, chai's equal): it passes for undefined only because of that. (Elsewhere no-null reports == null; where this rule does, it says more, so that report goes.)

Left alone: anything whose type may hold null or isn't known. A DOM lookup keeps its null (and toBeNull()), and outside data its === null:

✓ Accepted0 diagnostics
ts
import assert from "node:assert/strict";
import { test } from "node:test";

export function isMissing(value: unknown): boolean {
  return value === null || value === undefined;
}

test("the dialog is closed", () => {
  assert.equal(document.querySelector("dialog[open]"), null);
});

What counts as "no null": there is no type checker, so the types are read from the declarations: a parameter's or variable's declared type (or what a const or let is initialized with), a field of a type alias, interface or class (also an inherited one), a function's or method's declared return type (await of a Promise<Option<T>> too), an element of an array, a Map#get, across imports and re-exports. Option<T>, T | undefined, field?:, primitives, object types and the platform's classes have no null. Not followed: types that come from a package, any and unknown, a generic T (first<T>(items): Option<T> may be given nulls), conditional and mapped types, and types declared in a function.

Recognised assertions: node:assert (and node:assert/strict) equal, strictEqual, deepEqual, deepStrictEqual and their not... forms, called on the module or imported by name; chai's assert (also vitest's) equal, strictEqual, deepEqual, isNull, isNotNull; vitest's and jest's expect(x) with toBe, toEqual, toStrictEqual, toBeNull and .not.

Quick fixes: Compare with undefined (x === null to x === undefined, null to undefined in an assertion), Use toBeUndefined() for toBeNull(), Use toBeDefined() for .not.toBeNull(), and chai's isUndefined() / isDefined(). The title says what happens to the result: (the same as now) for x == null and a loose assertion, which already passed for undefined; (was always false), (was always true), (the assertion always failed) or (the assertion always passed) where the test now does something.

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