Use explicit patterns when dealing with potentially null or undefined values to prevent runtime errors and improve code clarity: 1. **Mark optional properties with the `?` operator** in TypeScript interfaces when values might be undefined:
Use explicit patterns when dealing with potentially null or undefined values to prevent runtime errors and improve code clarity:
?
operator in TypeScript interfaces when values might be undefined:// Bad
interface GuideArticleProps {
content: string
mdxOptions: SerializeOptions
}
// Good
interface GuideArticleProps {
content?: string
mdxOptions?: SerializeOptions
}
// Instead of allowing properties to be undefined and checking later
// Make them required in the interface/type definition
{
status: edgeFunctionsStatus?.healthy,
isSuccess: edgeFunctionsStatus?.healthy,
}
// 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.
Enter the URL of a public GitHub repository