The result of a call that can return null or undefined is used without a check
What it finds
localStorage.getItem("k").length, find(items, id).name where find ends in return undefined: a function returns null or undefined on some path, and its caller uses the result without a test, which throws a TypeError on that path.
Calls go to functions and methods of the same file (methods through this) and to functions of the language and the DOM that return null or undefined when they find nothing, in the table models/null.txt. A function counts as returning null or undefined only where it writes one or returns the result of such a call; falling off the end is not evidence, and a result that is null only when an argument is, as in if (x == null) return x, is not either. Also reported: passing null or undefined, or such a result, to a parameter that the callee uses on every path before any test. Optional chaining, ??, &&, ||, conditional expressions, tests for truth and non-null assertions (x!) are understood. Lookups of elements in the DOM (getElementById, querySelector, closest) are not reported: they look up the page's own elements, and code that uses the result directly does so on purpose.
From the rule's source, null_dereference_across_calls.rs.
interface User { id: string; name: string } function find(users: User[], id: string): User | undefined { for (const u of users) { if (u.id === id) { return u; } } return undefined;} export function nameOf(users: User[], id: string) { return find(users, id).name;tx0 reports: find(...) can return null or undefined} export function optional(users: User[], id: string) { return find(users, id)?.name;} export function checked(users: User[], id: string) { const u = find(users, id); if (!u) { throw new Error(id); } return u.name;} export function defaulted(users: User[], id: string) { const u = find(users, id) ?? { id, name: "" }; return u.name;} export function guarded(users: User[], id: string) { const u = find(users, id); return u && u.name;} export function ternary(users: User[], id: string) { const u = find(users, id); return u !== undefined ? u.name : "";} export function asserted(users: User[], id: string) { return find(users, id)!.name;} export function title() { // The page's own elements: a lookup that finds nothing is a broken page, not a case to handle. return document.querySelector("h1").textContent;} export function stored() { return localStorage.getItem("theme").length;tx0 reports: localStorage.getItem(...) can return null} export function storedChecked() { const v = localStorage.getItem("theme"); return v ? v.length : 0;} export function version(text: string) { const m = /v(\d+)/.exec(text); return m[1];tx0 reports: RegExp.exec(...) can return null} function same(x: string | null) { if (x === null) { return x; } return x.trim();} export function passesThrough(text: string) { return same(text).length;} Turn it on, off or suppress it
The rule runs from the all tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/ts:null-dereference-across-calls" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/ts:null-dereference-across-calls] reason="..."tx0 explain tx0/ts:null-dereference-across-calls shows the same in the terminal. Found a false positive? Report it.