Back to all reviewers

Use guards over assertions

apache/airflow
Based on 2 comments
TSX

Prefer explicit type guards and null checks over TypeScript's type assertions (`as`) when handling potentially null or undefined values. Type assertions bypass TypeScript's type checking system and can lead to runtime errors.

Null Handling TSX

Reviewer Prompt

Prefer explicit type guards and null checks over TypeScript’s type assertions (as) when handling potentially null or undefined values. Type assertions bypass TypeScript’s type checking system and can lead to runtime errors.

Instead:

  1. Use nullish coalescing operator (??) to provide default values
  2. Implement explicit type guards with Array.isArray() and property checks
  3. Use type predicates for more complex validations

Example (Before):

const menuPlugins = (useConfig("plugins_extra_menu_items") as Array<ExternalViewResponse>) ?? [];

Example (After):

const menuItems = useConfig("plugins_extra_menu_items");
const menuPlugins = Array.isArray(menuItems) ? menuItems : [];

// Or with additional type validation:
if (
  Array.isArray(menuItems) &&
  menuItems.every(item => "name" in item && "href" in item)
) {
  // Safe to use menuItems here
}

This approach ensures runtime safety beyond compile-time type checking and reduces the risk of unexpected null reference exceptions.

2
Comments Analyzed
TSX
Primary Language
Null Handling
Category

Source Discussions