Skip to content

compare-misses-undefined ​

A test that compares a value with "", 0 or 0n (id !== "", count === 0, n != 0n) where the value may be undefined and the test doesn't say what happens then. Reported in if, while, for, ?:, && and ||, !, and the callbacks of filter, find, some, every. On by default, as an error.

Why: undefined !== "" is true and undefined === "" is false, so a missing value passes a !== test and takes the other branch of a === test, without a word in the code. It is the mistake that replacing if (id) with if (id !== "") makes when id is optional: the old test was false for undefined, the new one is true. One project upgrading found 13 such tests, written by an agent following a quick fix. Every one compiled, and every command without a selected unit was refused:

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

type Game = { readonly units: ReadonlyMap<string, string> };

export function refuse(game: Game, unitId?: string): boolean {
  return unitId !== "" && !game.units.has(unitId ?? "");
}

export function label(name: Option<string>): string {
  return name === "" ? "(no name)" : `Name: ${name}`;
}

✅ Accepted: "" becomes undefined where the value comes in, and the code after tests one thing

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

type Game = { readonly units: ReadonlyMap<string, string> };

/** The unit a command names: none when the field is missing or empty. */
export function unitOf(field?: string): Option<string> {
  return field === "" ? undefined : field;
}

export function refuse(game: Game, unitId: Option<string>): boolean {
  return unitId !== undefined && !game.units.has(unitId);
}

✅ Also accepted: both halves, said, where the value can still be ""

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

type Game = { readonly units: ReadonlyMap<string, string> };

export function refuse(game: Game, unitId?: string): boolean {
  return unitId !== undefined && unitId !== "" && !game.units.has(unitId);
}

export function label(name: Option<string>): string {
  return name === undefined || name === "" ? "(no name)" : `Name: ${name}`;
}

What counts ​

  • Reported: x === v, x !== v, x == v, x != v with v one of "", '', ``, 0, 0n, where x is a name, a field, an element, a call or an optional chain whose type includes undefined: an optional parameter or field (id?: string), T | undefined, Option<T>, a?.b, map.get(k), list.find(...), an index of a dictionary, and types declared in other files.
  • Left alone where the code already says what undefined does:
    • the same test also compares x with undefined or null, uses typeof x, tests x itself (x && x !== "a"), or calls isSome(x) / isNone(x), in either order;
    • x, or the start of it, is proven defined there, as TypeScript narrows it (what is followed);
    • the branch a missing x takes gives x back: x === "" ? undefined : x;
    • (x ?? "") !== "", where ?? already decided.
  • Where the type isn't known, nothing is reported: a function's result that isn't declared nor plain (what is read off), a package's value, unknown. The rule doesn't guess.

What is followed ​

A value TypeScript has narrowed is defined where it is read, and so is a member of it: b.plots.length inside if (b.plots !== undefined) is a number. The rule follows:

  • the test of an enclosing if or ?: branch, the left of && / ||, and an earlier if (x === undefined) return; (or continue, break, throw) in the same or an enclosing block;
  • a test of a longer path: x.y !== undefined, x?.y > 0 and x?.some(f) (truthy) each mean x was there;
  • a name, a field (this.x, a.b.c), an element (xs[i], dict[key]) and an optional chain (a?.b is a.b);
  • a local copied from a narrowed path, and one destructured from it (const books = shelf.books, const { books } = shelf);
  • a path assigned just before: after shelf.books = kept.filter(f); it is what was assigned.
✓ Accepted0 diagnostics
ts
import type { Option } from "@evobug_com/safe-ts-std/result";

type Shelf = { readonly books: Option<readonly number[]> };

export function isEmpty(shelf: Shelf, next: () => Option<readonly number[]>): boolean {
  if (shelf.books !== undefined) {
    const books = shelf.books;
    if (books.length === 0 || shelf.books.length > 64) return true;
  }
  let route: Option<readonly number[]> = undefined;
  route = next();
  return route !== undefined && route.length === 0;
}

What undoes it, between the test and the read (and in all of a loop around the read that started after the test, which runs it again):

  • a write to the path or a part of it (shelf = other, shelf.books = load(), delete shelf.books, [shelf.books] = …, shelf[key] = …), or to a name in its keys (i += 1 for xs[i]);
  • a method call on a part of it (shelf.reset()), or a call that gets a part of it (clear(shelf)); shelf.books.push(x) can't make shelf.books missing and keeps it;
  • any call, await or yield where something else may write the path meanwhile: its root is imported, global or declared outside the function, or a closure or another function writes it or hands it on; an await or yield also undoes any field path, since other code runs then;
  • a function between: the read inside a callback that may run later isn't narrowed by a test outside it (except a name nothing assigns). A callback an array method runs during its call (forEach, map, filter, some, every, find…, reduce, sort) keeps it, with all of its body counted as code between.

What it can't see: one object reached through two names (const other = shelf; other.books = undefined, or two parameters holding the same object), a getter, setter, proxy or iterator running code during a read, and a method named like an array's on an object that keeps its callback for later. TypeScript doesn't see those either.

Quick fixes ​

TestFixes
x !== ""Make undefined fail too (x !== undefined && x !== ""), Let undefined pass (the same as now) (x === undefined || x !== "")
x === ""Treat undefined as "" (x === undefined || x === ""), Test for "" only (the same as now) (x !== undefined && x === "")
f(…) !== "" (read through a call)Make undefined fail too ((f(…) ?? "") !== ""), so the call runs once

The second fix keeps what the code does now and writes it out, for the places where it was meant.

Related: strict-truthiness reports the test for truth that this comparison often replaced, and offers both halves where the value may be undefined. Guide: Proof types.

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