number-parse-nan
Number(x), parseInt(x), parseFloat(x) or +x on a string, Date.parse(text), or a number read from new Date(text), used before anything checks the result for NaN. On by default, as a warning.
Why: text that isn't a number becomes NaN, and nothing says so. NaN goes through arithmetic (NaN * 2 is NaN), every comparison with it is false (NaN > 0, NaN <= 0, NaN === NaN), and JSON.stringify writes it as null. A typo in a query string becomes a page number that is neither positive nor negative, or a price of null in the database. The other surprises make it worse: Number("") is 0, and parseInt("12px") is 12. Guide: Errors.
✕ Rejectednumber-parse-nan
ts
export function pageSize(query: URLSearchParams): number {
const size = Number(query.get("size") ?? "20");
return Math.min(size, 100) + size % 10;
}✅ Accepted: parsed into a Result
✓ Accepted0 diagnostics
ts
import { parseInteger } from "@evobug_com/safe-ts-std/number";
import { isErr } from "@evobug_com/safe-ts-std/result";
export function pageSize(query: URLSearchParams): number {
const size = parseInteger(query.get("size") ?? "20");
return isErr(size) || size < 1 ? 20 : Math.min(size, 100);
}✅ Also accepted: Number(...) checked before use
✓ Accepted0 diagnostics
ts
export function pageSize(query: URLSearchParams): number {
const size = Number(query.get("size") ?? "20");
if (!Number.isSafeInteger(size) || size < 1) return 20;
return Math.min(size, 100);
}A bigint never gives NaN, so timing with process.hrtime.bigint() needs no check:
✓ Accepted0 diagnostics
ts
export function elapsedMs(start: bigint): number {
return Number(process.hrtime.bigint() - start) / 1e6;
}Neither does a number, or a boolean for Number(...) (parseInt(true) is NaN): !!x, a comparison, key in object, a field or parameter declared boolean, also in a type from another file. And a capture group of a regex match that is only digits: (\d+), ([0-9]{4}), (-?\d+), outside any | or optional part, from re.exec(text), text.match(re) or a for (const m of text.matchAll(re)), read as m[1] or destructured (const [, id] = m).
✓ Accepted0 diagnostics
ts
type Task = { done: boolean; title: string };
export function doneFirst(a: Task, b: Task): number {
return Number(b.done) - Number(a.done);
}
export function invoiceNumber(name: string): number {
const m = /^invoice-(\d+)$/.exec(name);
if (!m) return 0;
return Number(m[1]) + 1;
}
export function issueNumber(text: string): number {
const [, id = ""] = /^#(\d+)$/.exec(text) ?? [];
return Number(id) + 1;
}A default on the destructured group is used only when there is no match at all (?? []). Number("") and +"" are 0, not NaN, so a default of "" is accepted for them, and so is a default of digits. That is a choice: this rule is about NaN, and 0 is a number. When 0 is a wrong value too (an id that can't be 0), the default hides the missing match: check the match instead (if (!m) return ...). parseInt("") and parseFloat("") are NaN, so for them a default of "" is still reported.
A check is Number.isNaN, Number.isFinite, Number.isInteger, Number.isSafeInteger, or the global isNaN/isFinite, on the variable holding the result. x || fallback and x | 0 also count, since they replace NaN; x ?? fallback doesn't (NaN isn't undefined).
parseNumber(text) and parseInteger(text) from @evobug_com/safe-ts-std/number return a Result<number, NumberError> that says why ("empty", "not a number", "not finite", "not an integer", "not a safe integer"). They accept decimal notation only, with whitespace around it: no empty text, no trailing characters, no hex, no Infinity, and parseInteger only safe integers (like SafeInt in JSON).
Dates made from text
new Date(text) doesn't fail on text that isn't a date: it makes an Invalid Date, and every number read from it (getTime(), getFullYear(), valueOf(), +date, date - other) is NaN, and every comparison with it (date < deadline, new Date(a) > new Date(b)) is false. Date.parse(text) gives the NaN directly.
✕ Rejectednumber-parse-nan
ts
export function age(birthday: string, now: Date): number {
const born = new Date(birthday);
return now.getFullYear() - born.getFullYear();
}✅ Accepted: an invalid date is an error the caller handles
✓ Accepted0 diagnostics
ts
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { err, type Result } from "@evobug_com/safe-ts-std/result";
export type DateError = Enum<{ NotADate: { text: string } }>;
export const DateError = constructors<DateError>();
export function age(birthday: string, now: Date): Result<number, DateError> {
const born = new Date(birthday);
if (Number.isNaN(born.getTime())) return err(DateError.NotADate({ text: birthday }));
return now.getFullYear() - born.getFullYear();
}ts
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { err, type Result } from "@evobug_com/safe-ts-std/result";
export type DateError = Enum<{ NotADate: { text: string } }>;
export const DateError = constructors<DateError>();
export function age(birthday: string, now: Date): Result<number, DateError> {
const born = new Date(birthday);
if (Number.isNaN(born.getTime())) return err(({ tag: "NotADate", text: birthday } satisfies DateError as DateError));
return now.getFullYear() - born.getFullYear();
}A check of the date is Number.isNaN(date.getTime()) (or another check above of date.getTime(), date.valueOf(), +date, or of date itself: isNaN(date)), or String(date) === "Invalid Date"; a number read from it and kept (const ms = date.getTime()) is checked like any parsed number. A date from the clock (new Date()), from a number of milliseconds (new Date(ms), new Date(Date.now() - 1000), a field declared number, also in a type from another file), from its parts (new Date(2026, 0, 1)), a copy of a date (new Date(other) with other: Date) and an ISO date literal (new Date("2026-01-01")) are valid, and not reported. That date.toISOString() throws on an invalid date is unhandled-failure's: a date used both ways gets each finding once, and one check of it clears both.
Quick fix: Use parseNumber(…) for Number(x), parseFloat(x) and +x; Use parseInteger(…) for parseInt(x) and parseInt(x, 10) (another radix has no fix). The result is then a Result, so tsc shows where it still needs an isErr check.
Paths: uses and checks are followed along the paths the code can take (the flow layer): a check in one branch of an if doesn't cover a use after the other branch, a use on the next round of a loop comes after a parse at the end of the round, and a new value assigned to the variable ends what the parse left (let n = Number(text); n = 0;). A use or a check inside a callback is taken where the callback is made, in source order.
What the rule can't see: a check that only holds together with another condition (if (text !== "" && Number.isNaN(n)) return; leaves the path where text is "" unchecked, and Number("") is 0, so that one is reported though it can't be NaN). A comparison written to come out right for NaN (if (!(until > now)) return) is still a use. A date is followed in the const it is made into; one stored in a field or an object isn't. new Date(x) of a value whose type the checker can't find (a function's result that is neither declared nor plain, a package's value) is taken for text, and reported like Number(x) of one. A result passed to a function, used in a template string or as an object key isn't a use (the function may be the check), and neither is a value that arrives from elsewhere already parsed. +x is only reported when x is declared as a string. Number(x) is not reported when x is a number or boolean (above; parseInt(x) and parseFloat(x) only for a number), or is a bigint: a literal (10n), BigInt(...), a .bigint() call (process.hrtime.bigint()), a binding declared : bigint or initialized with one, or -, *, /, % or ** with one on either side (+ only with bigints on both sides, since it concatenates a string).