When handling possibly-null/undefined/empty inputs, be explicit about nullability and keep code aligned with the promised types/semantics.
Practical rules:
??) over falsy checks (||) when false (or 0) is a valid value that must not be treated as “missing”.undefined when the function claims string).number | string) via a shared parser or an explicit typeof check.null defaults; add/propagate types so nullability doesn’t collapse inference.Example pattern:
function getStylesFromDbInfo(dbInfoItem) {
return dbInfoItem?.styles ?? ''; // always a string
}
function useHtmlLabels(node, config) {
// preserves false when it matters; only falls back on null/undefined
return node.useHtmlLabels ?? config.flowchart.htmlLabels;
}
function getTasks(rawTasks, dateRange) {
if (dateRange === '') return rawTasks; // early return on empty input
// parse defensively
const [startStr, endStr] = dateRange.split(',');
// only use pieces if present
}
This reduces null/reference bugs and prevents subtle behavior changes caused by incorrect falsy-vs-nullish handling or mismatched return contracts.
Enter the URL of a public GitHub repository