import type { Falsy, NonFalsy } from "@samual/types" type Nullish = undefined | null type MessageFn = (value: T) => string export class AssertError extends Error {} Object.defineProperty(AssertError.prototype, `name`, { value: `AssertError` }) /** * Assertion function that ensures value is [truthy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy). * @throws If value is [falsy](https://developer.mozilla.org/en-US/docs/Glossary/Falsy). */ export function assert( value: T, message: string | MessageFn = "Assertion failed" ): asserts value { if (!value) throw new AssertError(typeof message == `string` ? message : message(value as T & Falsy)) } /** * Assertion function that ensures value is not [nullish](https://developer.mozilla.org/en-US/docs/Glossary/Nullish). * @throws If value is nullish. */ export function assertNonNull( value: T, message: string | MessageFn = "Non-null assertion failed" ): asserts value is NonNullable { assert(value != null, typeof message == `string` ? message : () => message(value as T & Nullish)) } /** * An alias for {@linkcode assert()} but with a type signature that explicitly narrows with {@linkcode NonFalsy<>}. * * This is useful because in this code: * ```ts * function foo(value: T) { * assert(value) * * value * // ^? (parameter) value: NonNullable * } * ``` * * TypeScript incorrectly narrows the type of `value` with `NonNullable<>` when it should be using something like * {@linkcode NonFalsy<>}. * * ```ts * function foo(value: T) { * assertTruthy(value) * * value * // ^? (parameter) value: NonFalsy * } * ``` * @alias */ export const assertTruthy: (value: T, message?: string | MessageFn) => asserts value is NonFalsy = assert /** * Assertion function that ensures value is not [nullish](https://developer.mozilla.org/en-US/docs/Glossary/Nullish). * @throws If value is nullish. * @returns Given value. */ export const expect = ( value: T, message: string | MessageFn = `Expectation failed` ): NonNullable => { assertNonNull(value, message) return value } /** * Assertion function that ensures value is [truthy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy). * @throws If value is [falsy](https://developer.mozilla.org/en-US/docs/Glossary/Falsy). * @returns Given value. */ export const expectTruthy = ( value: T, message: string | MessageFn = `Truthy expectation failed` ): NonFalsy => { assertTruthy(value, message) return value } export function unreachable(message: string = "Hit unreachable"): never { throw new AssertError(message) } if (import.meta.vitest) { const { test, expect: vitestExpect } = import.meta.vitest test(`assert()`, () => { assert(true) vitestExpect(() => { assert(false) }).toThrow(AssertError) }) test(`assertNonNull()`, () => { assertNonNull(false) vitestExpect(() => { assertNonNull(undefined) }).toThrow(AssertError) }) test(`expect()`, () => { vitestExpect(expect(false)).toBe(false) vitestExpect(() => expect(undefined)).toThrow(AssertError) }) test(`expectTruthy()`, () => { vitestExpect(expectTruthy(true)).toBe(true) vitestExpect(() => expectTruthy(false)).toThrow(AssertError) }) test(`unreachable()`, () => { vitestExpect(() => unreachable()).toThrow(AssertError) }) }