export function invariant(condition: unknown, message: string | (() => string)): asserts condition {
if (condition) return;
const msg = typeof message === 'function' ? message() : message;
throw new Error(`Invariant failed: ${msg}`);
}
function processUser(u: { id: number; email: string | null }) {
invariant(u.email, 'user must have an email');
// u.email is narrowed to `string` here — TS understands `asserts condition`
sendEmail(u.email);
}
Create a free account and build your private vault. Share publicly whenever you want.