Back to all reviewers

Use semantic null handling

mastodon/mastodon
Based on 2 comments
TypeScript

When designing APIs and data structures, be intentional about null and undefined usage. Use null and undefined with distinct semantic meanings rather than interchangeably. For example, undefined can indicate "not yet loaded" while null indicates "does not exist". However, avoid nullable types when simpler alternatives serve the same purpose - if an empty...

Null Handling TypeScript

Reviewer Prompt

When designing APIs and data structures, be intentional about null and undefined usage. Use null and undefined with distinct semantic meanings rather than interchangeably. For example, undefined can indicate “not yet loaded” while null indicates “does not exist”. However, avoid nullable types when simpler alternatives serve the same purpose - if an empty string conveys the same meaning as null (like “no description available”), prefer the empty string to simplify type handling.

Example of meaningful null/undefined distinction:

// Good: Semantic distinction
function useAccountId() {
  // undefined = not fetched yet, null = doesn't exist
  if (accountId) {
    return accountId;
  }
  return undefined; // or null, depending on semantic meaning
}

Example of avoiding unnecessary nullables:

// Instead of: avatar_description: string | null
// Prefer: avatar_description: string (empty string when no description)
interface AccountShape {
  avatar_description: string; // "" means no description
  header_description: string; // "" means no description
}
2
Comments Analyzed
TypeScript
Primary Language
Null Handling
Category

Source Discussions