Prompt
Null/undefined safety should prevent crashes and preserve correct semantics. Don’t hide missing required inputs with optional chaining or weak fallbacks.
Standards
- Validate required dependencies explicitly: If an object (e.g.,
themeVariables) is expected to exist, check it and emit a warning/error when it’s missing instead of silently continuing. - Use type-correct defaults, not
nullsentinels: If downstream code expects a color/string/number/boolean, provide a real default of that type—avoid?? nullunless the API explicitly supportsnull. - Avoid truthiness logic that changes meaning: Watch for patterns like
value || true(it always becomestrue). Compute booleans with correct precedence. - Be intentional about
nullvsundefined: Use??when you only want to treatnull/undefinedas “missing”, and add explicit branches when precedence matters.
Example pattern
function getThemeValue<T>(theme: any, key: string, fallback: T): T {
if (!theme) {
console.warn(`Missing themeVariables; using fallback for ${key}`);
return fallback;
}
const value = theme[key];
if (value === undefined || value === null) {
console.warn(`Missing theme value ${key}; using fallback`);
return fallback;
}
return value as T;
}
const quadrant1Fill = getThemeValue(themeVariables, 'quadrant1Fill', '#000000');
// Boolean example: avoid `anything || true`
const useHtmlLabels = node.useHtmlLabels ?? evaluate(config.htmlLabels ?? undefined) ?? true;
Applying these rules makes UI/config failures easier to diagnose, avoids runtime surprises from invalid sentinel values, and keeps boolean/null semantics correct.