uhyo/ts-array-length

Support assertion functions with assertion signatures, `assertLength` and others

Opened this issue · 0 comments

Hi,
it would be useful to support assertion function with assertion signatures, assertLength, assertMinLength, and assertNonEmpty. The implementation would be:

export function assertLength<T, N extends number>(
    arr: readonly T[],
    length: N,
): asserts arr is ReadonlyArrayExactLength<T, N> {
    if (!hasLength(arr, length)) {
        throw new Error(`Length is expected to be equal to ${length}, got ${arr.length}`)
    }
}