home / skills / andrelandgraf / fullstackrecipes / assert
This skill provides a TypeScript assertion helper for runtime type narrowing with descriptive errors, aiding robust input validation and early failure
npx playbooks add skill andrelandgraf/fullstackrecipes --skill assertReview the files below or copy the command above to add this skill to your agents.
---
name: assert
description: TypeScript assertion function for runtime type narrowing with descriptive error messages. Based on tiny-invariant.
---
# Assertion Helper
To set up Assertion Helper, refer to the fullstackrecipes MCP server resource:
**Resource URI:** `recipe://fullstackrecipes.com/assert`
If the MCP server is not configured, fetch the recipe directly:
```bash
curl -H "Accept: text/plain" https://fullstackrecipes.com/api/recipes/assert
```
This skill provides a small TypeScript assertion function for runtime type narrowing with clear, descriptive error messages. It is designed as a drop-in lightweight helper inspired by tiny-invariant, optimized for clarity and predictable runtime behavior. Use it to enforce invariants and narrow types at runtime in production web and AI apps. The implementation is minimal, with a focus on developer ergonomics and readable failure output.
At runtime the function checks a boolean condition and throws a descriptive Error when the condition is falsy. When used in TypeScript code, the signature acts as an assertion function, allowing the compiler to narrow types after a successful check. The message parameter accepts a string or a function that returns a string for lazily computed messages, reducing overhead when assertions pass.
Does this throw a specific error type?
It throws a standard Error with the provided descriptive message so typical error handling and reporting works out of the box.
Will message strings be evaluated when assertions pass?
If you provide a function that returns a string, it is evaluated lazily only when the assertion fails; plain strings are always created before the call.