<!--
title: avoid non-null assertions
domain: ai-agents
topic: Null Handling
language: TypeScript
source: google-gemini/gemini-cli
updated: 2025-07-24
url: https://awesomereviewers.com/reviewers/gemini-cli-avoid-non-null-assertions/
-->

Avoid using the non-null assertion operator (`!`) and instead use union types or explicit null checks for better type safety. Non-null assertions bypass TypeScript's null safety checks and can lead to runtime errors if the assumption is incorrect.

**Preferred approach - Use union types:**
```typescript
// Instead of using non-null assertion
return {
  filePath,
  relativePathForDisplay, 
  fileReadResult: fileReadResult!.llmContent  // ❌ Unsafe
};

// Use union types to make success cases explicit
type FileResult = 
  | { success: true; fileReadResult: FileReadResult }
  | { success: false; reason: string };

// TypeScript now properly narrows the type
if (fileResult.success) {
  // fileReadResult is guaranteed to exist here
  return fileResult.fileReadResult.llmContent; // ✅ Safe
}
```

**When explicit checks are safer:**
```typescript
// Instead of non-null assertion
return this.contentGenerator!; // ❌ Bypasses safety

// Use explicit null check with clear error handling
if (!this.contentGenerator) {
  throw new Error('Content generator not initialized');
}
return this.contentGenerator; // ✅ Safe and clear
```

This approach makes null handling explicit in the type system and prevents runtime errors from incorrect assumptions about null values.
