invariant

Enforce runtime safety for nullable values in TypeScript code.

Updated Jan 1, 2026
One-click install
npx skills add https://github.com/ViewableGravy/better-ecs --skill invariant
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: invariant
Source: https://github.com/ViewableGravy/better-ecs/tree/main/.github/skills/invariant
Command: npx skills add https://github.com/ViewableGravy/better-ecs --skill invariant

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill enhances code reliability by replacing unsafe type-only assertions with robust runtime checks, ensuring that nullable values are handled correctly and preventing runtime errors.

Core Features & Use Cases

  • Safe Type Narrowing: Use invariant(value, "message") to assert that a value is not null or undefined at runtime, which also narrows its type in TypeScript.
  • Avoids Non-Null Assertions: Replaces potentially dangerous ! and as operators with explicit runtime checks.
  • Handles Falsy Values Correctly: Provides clear guidance on when to use invariant versus explicit guard clauses for values like 0, "", or false.
  • Use Case: When fetching user data, ensure the user object is present before accessing its properties, preventing application crashes.

Quick Start

Use the invariant skill to assert that the 'user' variable is not null or undefined.

Frequently Asked Questions about invariant

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

FAQPage Schema
How do I enforce runtime type safety for nullable values in TypeScript?

You can enforce runtime type safety for nullable values by using `invariant` assertions. This mechanism checks value existence at runtime while simultaneously narrowing the TypeScript type, replacing unsafe non-null assertions and type casts.

How do I handle falsy values like 0 or empty strings with invariant assertions?

For falsy values like `0`, `""`, or `false`, you should use explicit guard clauses instead of `invariant`. The `invariant` function is designed for values where falsy results are not expected to be valid states.

Can I pass a nullable value directly to the invariant function?

Yes, you can pass a nullable value directly to the `invariant` function when falsy values are not expected in your logic flow. This asserts the value is not null or undefined at runtime and narrows its type safely.

What is the best way to prevent runtime errors when accessing fetched user object properties in TypeScript?

The best way to prevent runtime errors when fetching user data is to use `invariant(value, "message")`. This ensures the user object is present before accessing its properties, replacing dangerous type casts with robust runtime checks.

When should I not use the invariant approach for type narrowing?

You should not use the `invariant` approach when your application logic considers falsy values like `0`, `""`, or `false` to be valid. In these edge cases, explicit guard clauses are necessary to avoid incorrectly throwing errors.