-
Notifications
You must be signed in to change notification settings - Fork 0
/
assert_non_nullable.ts
29 lines (27 loc) · 979 Bytes
/
assert_non_nullable.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license.
// This module is browser compatible.
import { isNonNullable } from "https://deno.land/x/[email protected]/is_non_nullable.ts";
import type { ErrorConstructorLike } from "./types.ts";
/** Assert the input is not `null` or `undefined`.
* @param input - Any input.
* @example
* ```ts
* import { assertNonNullable } from "https://deno.land/x/assertion@$VERSION/assert_non_nullable.ts";
* import {
* assertFalse,
* assertThrows,
* } from "https://deno.land/std/testing/asserts.ts";
* assertFalse(assertNonNullable(""));
* assertThrows(() => assertNonNullable(null));
* assertThrows(() => assertNonNullable(undefined));
* ```
*
* @throws {Error} If the input is `null` or `undefined`.
*/
export function assertNonNullable<T>(
input: T,
msg?: string,
constructor: ErrorConstructorLike = Error,
): asserts input is NonNullable<T> {
if (!isNonNullable(input)) throw new constructor(msg);
}