1.2.0
New: Get message
function
The message
provided to invariant
can now be a function
that returns a string
- export default function invariant(condition: any, message?: string): asserts condition;
+ export default function invariant(condition: any, message?: string | (() => string)): asserts condition;
Using a function
that returns a string
is helpful in cases where your message
is expensive to create. By using a function
you only need to create the message
when you need it.
import invariant from 'tiny-invariant';
invariant(value, () => getExpensiveMessage());
Keep in mind, that ideally the second argument to invariant
is stripped away entirely in production builds.
We have a guide on how to do remove message
s for production builds