djankies/claude-configs/avoiding-non-null-assertions
Claude Code and agent skill by djankies/claude-configs
Trust tier 3
- community
Source check pending
Checking that the SKILL.md file is still in the repository.
Avoid non-null assertion operator (!) and use type-safe alternatives instead
Install
npx vskill@latest install djankies/claude-configs/avoiding-non-null-assertionsSKILL.md
The same SKILL.md was published earlier as djankies/claude-configs/avoiding-non-null-assertions.
Avoiding Non-Null Assertions
The non-null assertion operator (!) is deprecated in modern TypeScript because it bypasses type safety and can lead to runtime errors.
Why Avoid !
- Bypasses type safety: Tells TypeScript "trust me" without verification
- Runtime errors: Can cause
undefinedornullerrors at runtime - Maintenance burden: Makes refactoring dangerous
- No protection: Removes TypeScript's main benefit
Modern Alternatives
1. Optional Chaining (?.)
Bad:
Good:
2. Nullish Coalescing (??)
Bad:
Good:
3. Type Guards
Bad:
Good:
4. Early Return Pattern
Bad:
Good:
5. Custom Type Guards
Bad:
Good:
6. Narrowing with in Operator
Bad:
Good:
7. Array Methods with Type Safety
Bad:
Good:
8. Assertion Functions (TypeScript 3.7+)
Good:
DOM Element Access
Bad:
Good:
Or with assertion function:
When Is ! Acceptable?
Only in very rare cases where:
- You have exhaustively verified the value exists
- There's no other way to express it to TypeScript
- You document WHY it's safe
Even then, prefer assertion functions over !.
Migration Strategy
- Search for all uses of
!in codebase - Categorize by pattern (DOM access, array indexing, etc.)
- Replace with appropriate type-safe alternative
- Test thoroughly after each replacement
- Enable linting to prevent future uses
Compiler Configuration
Enable strict checks:
Summary
Never use `!` operator:
- Use
?.for optional chaining - Use
??for default values - Use type guards for narrowing
- Use assertion functions when validation is needed
- Let TypeScript protect you from null/undefined errors
Security
- Security scanPassed, 90 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingtypescript/skills/avoiding-non-null-assertions/SKILL.md
Popularity
- 7-day trend score
- 0
Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/djankies/claude-configs/avoiding-non-null-assertions)