alexreardon / tiny-invariant

A tiny invariant function

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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 || ''}`);

Closed by mistake