Prompt
Use explicit patterns when dealing with potentially null or undefined values to prevent runtime errors and improve code clarity:
- Mark optional properties with the
?operator in TypeScript interfaces when values might be undefined:
// Bad
interface GuideArticleProps {
content: string
mdxOptions: SerializeOptions
}
// Good
interface GuideArticleProps {
content?: string
mdxOptions?: SerializeOptions
}
- Prefer required properties when a value should always be present. This reduces defensive coding and makes contract expectations clear:
// Instead of allowing properties to be undefined and checking later
// Make them required in the interface/type definition
{
status: edgeFunctionsStatus?.healthy,
isSuccess: edgeFunctionsStatus?.healthy,
}
- Add explicit null checks before accessing properties of potentially undefined objects:
// Bad - may cause errors if currentOrg is undefined
enabled: !['team', 'enterprise'].includes(currentOrg?.plan.id ?? ''),
// Good - explicitly check existence first
enabled: currentOrg !== undefined && !['team', 'enterprise'].includes(currentOrg?.plan.id ?? ''),
Consistently applying these practices helps prevent null reference errors and makes code behavior more predictable across the codebase.