When a value can be undefined/null, do not assume it exists—guard it before access and keep the type model consistent.
Rules
function f(params?: { message?: string | ((d: unknown[]) => string) }) {
const message =
typeof params?.message === "function"
? params.message
: errorUtil.toString(params?.message);
}
in, Object.keys, or nested properties, ensure the container object exists:
if (refSeen.def && key in refSeen.def) {
// safe
}
undefined (e.g., coercion failures), make sure downstream code treats that undefined as the expected “invalid” state and update types accordingly.: undefined can change runtime semantics ("prop" in obj). Prefer property absence when that’s the intended meaning.Outcome: fewer runtime TypeErrors and clearer, more intentional null/undefined handling across the codebase.
Enter the URL of a public GitHub repository