no-null
null is not used: absence is undefined (Option<T>).
Why: two "nothing" values means checking for one and receiving the other. With one, ?., ??, optional fields and default parameters all just work. Guide: Absence.
✕ Rejectedno-null
ts
export function middleName(names: readonly string[]): string | null {
return names.length > 2 ? names[1] ?? null : null;
}✓ Accepted0 diagnostics
ts
import type { Option } from "@evobug_com/safe-ts-std/result";
export function middleName(names: readonly string[]): Option<string> {
return names.length > 2 ? names[1] : undefined;
}null coming from outside (JSON, APIs, databases) is converted where it enters: parseJson<T> turns it into undefined for you, and value == undefined matches both when you check by hand.
Where outside code is what needs null, it isn't reported:
- an argument of a call to code outside the project, also inside an object or array literal written there:
Object.create(null),JSON.stringify(body, null, 2), a package's function, a method of a value that came from a package, or of one declared with a package's type (statement: StatementSync, a class field#insert: StatementSync, a local made from one, also asOption<...>). Not a callback written there: its body is your code again; - an event handler property set to
null(button.onclick = null), a field of a global (window.opener = null), and a field of a value a platform API returned (const tab = window.open(...), thentab.opener = null); - a value written where a package's or the platform's type is expected: a variable declared with one (
const widget: editor.IContentWidget = {...}),{...} satisfies DecorationOptions, the fields of an object literal there (also nested), and what its functions return (const storage: Pick<Storage, "getItem"> = { getItem: () => null }).Pick,Omit,Partial,RequiredandReadonlyof such a type count as one. A class thatimplementsa package's interface: what its methods return, and its fields (getPosition() { return null; }in a Monaco content widget); - where a package's or the platform's type is expected, also as
value ?? nullor a branch of?:: returned from a function (or arrow) declared to return one ((p): SQLInputValue => p ?? null), pushed into or written in an array declared with such elements (params: SQLInputValue[]), a variable declared with one, a field of a package's object (group.parent = null, also for eachchild of group.children), an argument of a method of a value declared with the platform's type (gl.texImage2D(..., null)withgl: WebGL2RenderingContext); - the
nullin the type of a parameterel: Element | null(a package's or the platform's type) that only hands the value on to outside code or tests it (el === null,el?.id,if (el)): a helper taking whatquerySelectorgives. Returned, stored or passed to your own functions, itsnullwould spread, and it is reported; - a strict comparison,
value === nullorvalue !== null, which tests outside data for thenullit may hold. (value == nullis still reported;value == undefinedis the same test.) On a value whose type has nonull(anOption, a pack's safe version) the comparison is always false (or always true):no-null-check-for-undefinedreports that.
✓ Accepted0 diagnostics
ts
export function requestBody(scope: string): string {
// The OIDC `claims` parameter asks for a claim with an explicit `null`.
return JSON.stringify({ scope, claims: { id_token: { preferred_username: null } } });
}
export function isMissing(value: unknown): boolean {
return value === null || value === undefined;
}✓ Accepted0 diagnostics
ts
import type { SQLInputValue, StatementSync } from "node:sqlite";
import type { Option } from "@evobug_com/safe-ts-std/result";
export function addNote(insert: StatementSync, invoice: string, note: Option<string>): void {
insert.run(invoice, bound(note));
}
function bound(value: Option<SQLInputValue>): SQLInputValue {
return value ?? null;
}
export function unbind(gl: WebGL2RenderingContext): void {
gl.bindTexture(gl.TEXTURE_2D, null);
}✅ Accepted: the package's types ask for null
✓ Accepted0 diagnostics
ts
export const storage: Pick<Storage, "getItem"> = { getItem: () => null };
export function openLater(url: string): void {
const tab = window.open("", "_blank");
if (tab === null) return;
tab.opener = null;
tab.location.href = url;
}Your own types are still checked: const hint: { at: Position | null } = { at: null } is reported twice, in the type and in the value.
What the rule can't see: where a value of a package's type came from when nothing declares it (const child = pick(group) with pick your own function): child.parent = null is then reported. Declare the type (const child: Sprite = ...), or use the null-to-undefined codemod's report to see which ones it kept.
Quick fix: Replace null with undefined. Check comparisons afterwards: x === null and x === undefined are not the same test.