### Installation (Bash) Source: https://github.com/alexreardon/tiny-invariant/blob/master/README.md Provides instructions for installing `tiny-invariant` using both Yarn and npm package managers. This is a standard step for integrating the library into a project. ```bash # yarn yarn add tiny-invariant # npm npm install tiny-invariant --save ``` -------------------------------- ### Production Behavior and Bundle Optimization with tiny-invariant Source: https://context7.com/alexreardon/tiny-invariant/llms.txt Explains how tiny-invariant behaves in production environments (NODE_ENV === 'production'). Custom messages are removed to reduce bundle size. The example shows how Babel plugins and bundler configurations can further optimize the code by removing invariant calls entirely in production. ```typescript import invariant from 'tiny-invariant'; // Development (NODE_ENV !== 'production') invariant(false, 'Detailed error message for debugging'); // Error: Invariant failed: Detailed error message for debugging // Production (NODE_ENV === 'production') invariant(false, 'Detailed error message for debugging'); // Error: Invariant failed // (Custom message is stripped for smaller bundle size) // Bundle optimization with babel-plugin-dev-expression transforms: // Before: invariant(condition, 'My detailed message'); // After transformation: if (!condition) { if ('production' !== process.env.NODE_ENV) { invariant(false, 'My detailed message'); } else { invariant(false); } } // Final production build (after dead code elimination): if (!condition) { invariant(false); } ``` -------------------------------- ### Production Build Optimization (JavaScript) Source: https://github.com/alexreardon/tiny-invariant/blob/master/README.md Explains how `tiny-invariant` can be optimized for production builds. By using tools like `babel-plugin-dev-expression` and configuring bundlers, the error messages can be removed entirely, reducing the final bundle size. ```javascript if (!condition) { if ('production' !== process.env.NODE_ENV) { invariant(false, 'My cool message that takes up a lot of kbs'); } else { invariant(false); } } // After bundler optimization: // if (!condition) { // invariant(false); // } ``` -------------------------------- ### Basic Invariant Check (TypeScript) Source: https://github.com/alexreardon/tiny-invariant/blob/master/README.md Demonstrates the fundamental usage of `tiny-invariant`. It takes a condition and an optional message. If the condition is falsy, it throws an error. The message can be a string or a function returning a string. ```typescript import invariant from 'tiny-invariant'; invariant(truthyValue, 'This should not throw!'); invariant(falsyValue, 'This will throw!'); // Error('Invariant violation: This will throw!'); ``` -------------------------------- ### Truthy and Falsy Value Handling in invariant Source: https://context7.com/alexreardon/tiny-invariant/llms.txt Demonstrates the behavior of the `invariant` function with various JavaScript truthy and falsy values. Truthy values allow execution to continue, while falsy values trigger an error. ```typescript import invariant from 'tiny-invariant'; // All truthy values - do NOT throw invariant(1, 'positive number'); // passes invariant(-1, 'negative number'); // passes invariant(true, 'boolean true'); // passes invariant({}, 'empty object'); // passes invariant([], 'empty array'); // passes invariant(Symbol(), 'symbol'); // passes invariant('hello', 'non-empty string'); // passes // All falsy values - THROW Error invariant(undefined, 'undefined'); // throws: Invariant failed: undefined invariant(null, 'null'); // throws: Invariant failed: null invariant(false, 'false'); // throws: Invariant failed: false invariant(0, 'zero'); // throws: Invariant failed: zero invariant(-0, 'negative zero'); // throws: Invariant failed: negative zero invariant(NaN, 'NaN'); // throws: Invariant failed: NaN invariant('', 'empty string'); // throws: Invariant failed: empty string ``` -------------------------------- ### Type Narrowing with Invariant (TypeScript) Source: https://github.com/alexreardon/tiny-invariant/blob/master/README.md Demonstrates how `tiny-invariant` can be used for type narrowing in TypeScript and Flow. By asserting that a value is not null or undefined, the type system can infer a more specific type for subsequent operations. ```typescript const value: Person | null = { name: 'Alex' }; // type of value == 'Person | null' invariant(value, 'Expected value to be a person'); // type of value has been narrowed to 'Person' ``` -------------------------------- ### Basic invariant assertion in TypeScript Source: https://context7.com/alexreardon/tiny-invariant/llms.txt Demonstrates the core usage of the `invariant` function for asserting conditions in TypeScript. It shows how to use it with truthy and falsy values, with and without custom messages, and with template literals for dynamic messages. The function throws an error when the condition is falsy. ```typescript import invariant from 'tiny-invariant'; // Basic assertion with truthy value - does not throw const user = { id: 1, name: 'Alex' }; invariant(user, 'User must exist'); // Execution continues normally // Basic assertion with falsy value - throws Error invariant(false, 'This will throw'); // Error: Invariant failed: This will throw // Assertion without message - uses default invariant(null); // Error: Invariant failed // Using template literals for dynamic messages const userId = 42; invariant(userId > 0, `User ID must be positive, got: ${userId}`); // Lazy message evaluation for expensive computations invariant(condition, () => computeExpensiveErrorMessage()); // Message function only called if condition is falsy ``` -------------------------------- ### Invariant with Function Message (TypeScript) Source: https://github.com/alexreardon/tiny-invariant/blob/master/README.md Illustrates using a function to generate the error message. This is beneficial for expensive-to-create messages, as the function will only be called if the invariant fails. This optimizes performance by deferring message generation. ```typescript import invariant from 'tiny-invariant'; invariant(condition, `Hello, ${name} - how are you today?`); // Using a function is helpful when your message is expensive invariant(value, () => getExpensiveMessage()); ``` -------------------------------- ### Invariant with Template Literal Message (TypeScript) Source: https://github.com/alexreardon/tiny-invariant/blob/master/README.md Shows how `tiny-invariant` encourages the use of template literals for dynamic message formatting, which is more efficient than the `sprintf` style used by other libraries. This approach leverages modern JavaScript features. ```typescript import invariant from 'tiny-invariant'; invariant(condition, `Hello, ${name} - how are you today?`); ``` -------------------------------- ### Lazy Message Evaluation with tiny-invariant Source: https://context7.com/alexreardon/tiny-invariant/llms.txt Demonstrates how to use a function for error messages in tiny-invariant. The message computation function is only executed when the invariant condition fails, which is beneficial for expensive operations like serialization or logging. ```typescript import invariant from 'tiny-invariant'; // Expensive message computation only runs on failure function validateConfig(config: unknown) { invariant(config, () => { // This expensive serialization only happens if config is falsy return `Invalid config received: ${JSON.stringify(config, null, 2)}`; }); } // Practical example with database query validation async function getUser(id: string) { const user = await database.findUser(id); invariant(user, () => { // Log and build message only on failure logger.warn(`User lookup failed for ID: ${id}`); return `User not found with ID: ${id}`; }); return user; } // Message function is NOT called when condition is truthy const getMessage = jest.fn(() => 'error message'); invariant(true, getMessage); // getMessage was never called - no performance cost ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.