Back to all reviewers

Type-safe null handling

vercel/ai
Based on 6 comments
TypeScript

Use TypeScript's type system and modern JavaScript features to prevent null reference errors. **TypeScript type safety:** - Prefer `never` over `undefined` for properties that shouldn't have values

Null Handling TypeScript

Reviewer Prompt

Use TypeScript’s type system and modern JavaScript features to prevent null reference errors.

TypeScript type safety:

  • Prefer never over undefined for properties that shouldn’t have values
  • Use unknown instead of any when working with data of uncertain structure
  • Validate data with zod schemas instead of using type assertions (as)
  • Use proper type narrowing with type guards

Modern JavaScript null checks:

  • Use nullish coalescing (??) for default values: ```typescript // Instead of this: const citations = response.message.citations ? response.message.citations : [];

// Do this: const citations = response.message.citations ?? [];


- Use optional chaining (`?.`) for accessing properties on potentially undefined objects:
```typescript
// Instead of this:
const contentType = requestClone.headers.get('content-type') || '';
if (contentType.startsWith('multipart/form-data')) { /*...*/ }

// Do this:
const contentType = requestClone.headers.get('content-type');
if (contentType?.startsWith('multipart/form-data')) { /*...*/ }

Following these patterns leads to more robust, self-documenting code and fewer runtime errors from unexpected null/undefined values.

6
Comments Analyzed
TypeScript
Primary Language
Null Handling
Category

Source Discussions