When values are optional (undefined/null), external data can be malformed, or runtime internals may not exist, code should guard first and choose safe, backward-compatible defaults instead of relying on truthiness.
Apply this standard:
null, confirm typeof === 'object', and also guard against arrays via Array.isArray(...).false).id || '') and ensure downstream logic understands the sentinel.Example pattern:
function safeTabHandler(prompt) {
const clear = prompt?._clearUserInput;
const set = prompt?._setUserInput;
if (typeof clear !== 'function' || typeof set !== 'function') return; // no-op
clear();
set(/* ... */);
}
function validateParsed(parsed) {
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
return { ok: false, reason: 'module.yaml must be a non-array object' };
}
return { ok: true };
}
function interpretSuccess(handlerResult) {
// Missing `success` should remain backward compatible (default true)
return handlerResult?.success !== false;
}
This prevents null reference errors, avoids crashes on malformed input, and keeps behavior stable when optional fields are introduced or absent.