Accept generator function for error message
davidje13 opened this issue · comments
For dev-time error messages (and for those not using build-time rewriters), it would be nice if time-consuming calculations for error messages were only performed in the event of an error. This would be easy to achieve with minimal bloat using a lambda:
invariant(something, () => `my message with ${complex()} calculations`)
The build-time rewriters will be able to strip this with no changes necessary.
Internally:
throw new Error(`${prefix}: ${typeof message == 'function' ? message() : message || ''}`);
🤔
How does this look @davidje13? https://github.com/alexreardon/tiny-invariant/pull/76/files
Closed by mistake