Prompt
Use TypeScript’s type system and modern JavaScript features to prevent null reference errors.
TypeScript type safety:
- Prefer
neveroverundefinedfor properties that shouldn’t have values - Use
unknowninstead ofanywhen 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.