undefined-to-null
A serializer that writes null (or jsonNull) for every undefined: a JSON.stringify replacer that returns null for undefined, or a function that walks an object's keys (Object.entries, Object.keys, for...in) and does the same. An idiomatic rule: reported only with "safe-ts": { "idiomatic": true } in package.json.
Why: JSON.stringify leaves a key whose value is undefined out of the body; that is how "not sent" is spelled. A blanket rewrite sends every such key as null, so a field the code didn't set clears data on the server, and the code ends up with two spellings of "clear" (undefined and jsonNull). Write null only where the protocol asks for it, in the one body that needs it. Guide: Absence.
✕ Rejectedundefined-to-null
ts
import { jsonNull } from "@evobug_com/safe-ts-std/json";
export function toWire(body: Readonly<Record<string, unknown>>): Record<string, unknown> {
return Object.fromEntries(Object.entries(body).map(([key, value]) => [key, value === undefined ? jsonNull : value]));
}✅ Accepted: null where this request means "clear"
✓ Accepted0 diagnostics
ts
import { type JsonNull, jsonNull } from "@evobug_com/safe-ts-std/json";
import type { Option } from "@evobug_com/safe-ts-std/result";
type DueDatePatch = { readonly dueDate: string | JsonNull };
export function clearOrSet(due: Option<string>): DueDatePatch {
return { dueDate: due ?? jsonNull };
}x ?? null and x === undefined ? null : x elsewhere (one field, written where the body is built) aren't reported. A replacer is also a call JSON.stringify can throw out of (unhandled-failure).