Null/undefined handling must preserve intent across API boundaries and persistence layers. Treat undefined as “not provided/unchanged/omit”, and treat null (or an explicitly reset payload) as “clear/reset this value”. Ensure your type annotations and runtime normalization match what the receiving contract/storage layer expects, and lock the behavior with tests for clear/reset vs default transitions.
Apply:
null or a specific empty object/shape), not {} accidentally, and not an omitted field.undefined.null vs undefined: if code or defaults treat them differently (e.g., null should not be swallowed like undefined), ensure the function signature and call sites agree.Example pattern (clear vs omit):
// clear/reset vs omit
const payload = {
// If user cleared a field, send explicit reset value the contract expects:
baseUrl: userCleared ? null : undefined, // undefined => omit/leave unchanged
};
// persistence: match the storage encoding expected for clears
await client.command("session.update", {
sessionId,
...(metadataToClear ? { metadata: {} } : { metadata: updates.metadata }),
});
Also add focused tests for:
null being decoded as omittedimages -> []) and must not overwrite previous values unintentionally.