Back to all reviewers

prefer != null comparisons

nuxt/nuxt
Based on 3 comments
TypeScript

When checking for null or undefined values, prefer the concise `!= null` comparison over explicit checks or array-based methods. This pattern effectively handles both null and undefined while preserving other falsy values like `false`, `0`, or empty strings.

Null Handling TypeScript

Reviewer Prompt

When checking for null or undefined values, prefer the concise != null comparison over explicit checks or array-based methods. This pattern effectively handles both null and undefined while preserving other falsy values like false, 0, or empty strings.

The != null pattern is more readable and performant than alternatives:

// Preferred: Concise and clear
const hasCachedData = () => options.getCachedData!(key) != null

// Avoid: Verbose explicit checks
const hasCachedData = () => {
  const data = options.getCachedData!(key)
  return data !== null && data !== undefined
}

// Avoid: Array-based inclusion checks
const hasCachedData = () => ![null, undefined].includes(options.getCachedData!(key) as any)

For cases requiring additional type safety, combine null checks with type validation:

// Good: Null check with type validation
if (!rawVersion || typeof rawVersion !== 'string') {
  return
}

// Good: Explicit equality for specific values
if (result === true) {
  // Handle confirmed true, not just truthy
}

This approach maintains null safety while keeping code concise and avoiding unnecessary complexity in most common scenarios.

3
Comments Analyzed
TypeScript
Primary Language
Null Handling
Category

Source Discussions