Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/samualtnorman/lib. A collection of common utilities I need. www.npmjs.com/package/@samual/lib
Something went wrong. Try again.
12345678910111213141516171819import type { NonFalsy } from "."import { createErrorClass } from "./createErrorClass"
export const AssertError = createErrorClass(`AssertError`)
/** @example assert(typeof maybeString == "string", () => `Got ${typeof maybeString} instead of string`) */export function assert(value: any, message: (() => string) | string = `assertion failed`): asserts value { if (!value) throw new AssertError(typeof message == `string` ? message : message())
return value}
export default assert
/** Like typescript's non-null assertion operator (postfix `!`), but enforced at runtime. * @example ensure(objectOrUndefined, "Got undefined instead of object").property */export const ensure = assert as <T>(value: T, message?: (() => string) | string) => NonFalsy<T>