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 TypeScript’s type system and modern JavaScript features to prevent null reference errors.
TypeScript type safety:
never
over undefined
for properties that shouldn’t have valuesunknown
instead of any
when working with data of uncertain structureas
)Modern JavaScript null checks:
??
) 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.
Enter the URL of a public GitHub repository