tiny-invariant
A tiny invariant function
This package has a good security score with no known vulnerabilities.
Community Reviews
Lightweight assertion helper with solid defaults for runtime validation
From a security perspective, this is mostly safe territory. The library doesn't do anything fancy with user input—it just evaluates your condition and throws. However, you need to be careful about what you pass as the message parameter. If you're interpolating user data into error messages, you're responsible for sanitization. The library won't expose secrets on its own, but sloppy message construction can leak internal state or PII in error logs.
Dependency-wise, it's refreshingly minimal with zero runtime dependencies, reducing supply chain attack surface. The codebase is tiny enough to audit in minutes. My only real concern is that it doesn't provide type narrowing in TypeScript as effectively as assertion functions (asserts keyword), so you may need to pair it with type guards for proper type safety.
Best for: Projects needing lightweight runtime assertions with minimal bundle impact and simple precondition checking.
Avoid if: You need TypeScript type narrowing from assertions or require structured error handling with error codes and metadata.
Minimal assertion utility that does one thing well with zero overhead
The library strips messages in production builds when used with proper bundlers (Webpack, Rollup, etc.), which is critical for keeping bundle sizes down. Error messages are clear and include the failed condition in dev mode, making debugging straightforward. The API is dead simple: `invariant(condition, message)` - if false, it throws. No configuration needed, no initialization, no memory footprint to speak of.
One gotcha: it throws Error objects, not custom types, so you can't easily distinguish invariant failures from other errors in your error handling middleware without parsing message strings. Also, there's no built-in support for formatted messages with interpolation - you need template literals for that. For most use cases though, this simplicity is exactly what you want when you just need assertions without the weight of a full validation library.
Best for: Applications needing lightweight runtime assertions where bundle size matters and you don't need specialized error types.
Avoid if: You need typed error classes, structured logging integration, or complex error handling patterns that require distinguishing assertion failures.
Minimal, predictable assertion utility with good production defaults
The library has zero dependencies, which is a significant win for supply chain risk. It's a single file doing one thing, making it easy to audit. The source is straightforward TypeScript with no complex logic paths. However, the message-stripping behavior requires bundler cooperation and proper NODE_ENV configuration - if your build pipeline doesn't set this correctly, you'll ship full error messages to production.
For type safety, it includes TypeScript assertion signatures that properly narrow types after the invariant check, which prevents common validation bugs. The predictable throwing behavior (always Error instances) makes error handling straightforward in try-catch blocks.
Best for: Type-safe runtime assertions in TypeScript projects where you need predictable error behavior and production message sanitization.
Avoid if: You need structured error handling with error codes or don't have proper NODE_ENV build configuration.
Sign in to write a review
Sign In