@dts-stn/invariant
v1.0.0
Published
An invariant function
Downloads
3,407
Readme
invariant
An invariant function takes a value, and if the value is falsy then the invariant function will throw. If the value is truthy, then the function will not throw.
import { invariant } from '@dts-stn/invariant';
invariant(truthyValue, 'This should not throw!');
invariant(falsyValue, 'This will throw!');
// Error('Invariant violation: This will throw!');Why @dts-stn/invariant?
Unlike tiny-invariant, which strips error messages in production, @dts-stn/invariant preserves them. This is the fundamental reason for its creation, as these messages are vital for debugging production issues.
Error Messages
@dts-stn/invariant allows you to pass a string message, or a function that returns a string message. Using a function that returns a message is helpful when your message is expensive to create.
import { invariant } from '@dts-stn/invariant';
invariant(condition, `Hello, ${name} - how are you today?`);
// Using a function is helpful when your message is expensive
invariant(value, () => getExpensiveMessage());Type narrowing
@dts-stn/invariant is useful for correctly narrowing types for 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'API: (condition: any, message?: string | (() => string)) => void
conditionis required and can be anythingmessageoptionalstringor a function that returns astring(() => string)
