ts-invariant is a TypeScript-first implementation of the widely used `invariant(condition, message)` assertion pattern. It's designed for validating preconditions and ensuring consistent program state, primarily in development builds. The current stable version is 0.10.3. Actively maintained as part of the Apollo GraphQL ecosystem, its release cadence is tied to broader project needs rather than a fixed schedule. Key differentiators include robust TypeScript typing for compile-time safety, extended logging capabilities (`invariant.log`, `invariant.warn`, `invariant.error`), and configurable verbosity via `setVerbosity`. A significant feature is its compatibility with bundler plugins (like `rollup-plugin-invariant`, though that plugin is now archived), which can strip assertion messages from production bundles to reduce file size, making it a performance-conscious choice for assertions.
npm install ts-invariantVerified import paths — ran on the pinned version, not inferred.
Demonstrates importing `invariant`, `setVerbosity`, and `InvariantError`, showing basic assertion, logging levels, and the effect of verbosity settings. It also highlights `process.env.NODE_ENV` behavior.
Ensure `process.env.NODE_ENV` is correctly set to 'development' during development builds to retain messages. For production debugging, either temporarily disable stripping or rely on stack traces and code context.
When bundling for browsers, ensure your bundler (e.g., Webpack, Rollup, Vite) correctly defines or polyfills `process.env.NODE_ENV`. For Webpack, use `DefinePlugin`; for Rollup, use `@rollup/plugin-replace`.
Consider migrating to alternative bundler plugins or custom configurations that achieve similar dead-code elimination if you rely on stripping invariant messages. Verify continued compatibility if you remain on `rollup-plugin-invariant`.
Configure your bundler to provide a `process.env` polyfill or replacement. For Webpack, use `new webpack.DefinePlugin({ 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV) });`. For Rollup, use `@rollup/plugin-replace`.During development, this is a signal to fix the underlying condition. In production, if messages are not stripped, verify your bundler's configuration for `process.env.NODE_ENV` and the invariant stripping plugin.
Change your import statement from `import invariant from 'ts-invariant';` to `import { invariant } from 'ts-invariant';`. If using CommonJS, ensure transpilation or use `const { invariant } = require('ts-invariant');` (though ESM is preferred).No dependency data recorded yet.