Always provide safe defaults or fallbacks when working with optional or potentially undefined values. Avoid using non-null assertion operators (!
) on optional parameters, and ensure proper handling in type checking and value merging scenarios.
Key practices:
val(info || { props: {} })
instead of val(info!)
color: var(--ant-tooltip-color, ${tooltipColor})
instead of assuming the CSS variable existsExample:
// ❌ Dangerous - assumes info exists
return val(info!) as T;
// ✅ Safe - provides default
return val(info || { props: {} as Props }) as T;
// ❌ No fallback for missing CSS variable
color: 'var(--ant-tooltip-color)',
// ✅ Provides fallback
color: `var(--ant-tooltip-color, ${tooltipColor})`,
This prevents runtime errors and ensures predictable behavior when dealing with optional or missing values.
Enter the URL of a public GitHub repository