avoiding-non-null-assertions

Convert TypeScript non-null assertions to optional chaining and type guards.

Updated Nov 21, 2025
One-click install
npx skills add https://github.com/djankies/claude-configs --skill avoiding-non-null-assertions
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: avoiding-non-null-assertions
Source: https://github.com/djankies/claude-configs/tree/main/typescript/skills/avoiding-non-null-assertions
Command: npx skills add https://github.com/djankies/claude-configs --skill avoiding-non-null-assertions

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

The non-null assertion operator (!) bypasses TypeScript's safety checks and can lead to runtime errors. This Skill promotes safer alternatives like optional chaining, nullish coalescing, and type guards.

Core Features & Use Cases

  • Safe alternatives to ! (optional chaining, nullish coalescing)
  • Type guards to narrow types safely
  • Early returns and assertion functions for explicit safety

Quick Start

Replace uses of ! with safe alternatives and add a type guard for runtime safety.

Frequently Asked Questions about avoiding-non-null-assertions

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I eliminate non-null assertions in TypeScript code?

Non-null assertions (!) bypass TypeScript's type safety and risk runtime errors. Replace them with optional chaining (?.), nullish coalescing (??), type guards, or early returns to preserve strict null checking and catch errors at compile time.

What are safe alternatives to the ! operator in TypeScript?

Safe alternatives include optional chaining (?.) for property access, nullish coalescing (??) for default values, type guards and assertion functions for narrowing types, and early returns to validate values before use.

Why should I avoid non-null assertions in TypeScript?

The ! operator disables TypeScript's null-checking protections, allowing undefined or null values to slip through to runtime. Type-safe alternatives like optional chaining and type guards catch these errors at compile time instead.

How do I refactor legacy TypeScript code that uses ! patterns?

Identify ! usage across your codebase, convert to optional chaining, nullish coalescing, or type guards, add compile-time checks with strict null checks enabled, and verify with tests that no undefined/null errors reach runtime.

Can I use type guards to replace non-null assertions?

Yes. Type guards validate that a value is not null before use, narrowing the type safely. Combined with early returns or conditional logic, they provide compile-time safety without bypassing TypeScript's checks.

How do I safely access DOM elements without non-null assertions?

Use optional chaining to safely access DOM properties (e.g., `element?.textContent`), add type guards to verify element existence, or use early returns before accessing properties to ensure null-safety without assertions.