Back to all reviewers

prefer nullish coalescing operators

cloudflare/workerd
Based on 3 comments
TypeScript

Use nullish coalescing operators (`??`) and logical OR with safe defaults (`|| {}`) to handle null and undefined values more safely and concisely. This prevents runtime errors when accessing properties on potentially null/undefined objects and provides cleaner, more readable code.

Null Handling TypeScript

Reviewer Prompt

Use nullish coalescing operators (??) and logical OR with safe defaults (|| {}) to handle null and undefined values more safely and concisely. This prevents runtime errors when accessing properties on potentially null/undefined objects and provides cleaner, more readable code.

Instead of explicit null/undefined checks:

// Avoid
return this._eventsCount > 0 && this._events !== undefined
  ? Reflect.ownKeys(this._events)
  : [];

// Prefer
return this._eventsCount > 0 ? Reflect.ownKeys(this._events || {}) : [];

For numeric values, use nullish coalescing to distinguish between falsy values and null/undefined:

// Avoid - treats 0 as falsy
this.errno = options.errno || 1;

// Prefer - only coalesces null/undefined
this.errno = options.errno ?? 1;

This approach reduces boilerplate code while providing better null safety and more predictable behavior when dealing with optional properties and parameters.

3
Comments Analyzed
TypeScript
Primary Language
Null Handling
Category

Source Discussions