-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathassertions.ts
More file actions
24 lines (21 loc) · 631 Bytes
/
Copy pathassertions.ts
File metadata and controls
24 lines (21 loc) · 631 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
export function assertExhausted(x: never, description: string): never {
throw new TypeError(
`assertExhausted: ${description} was unexpectedly ${JSON.stringify(x)}`,
);
}
export function throwIfNullOrUndefined<T>(
x: T,
descriptionOfIt: DescriptionOrRedundanceHint<T>,
): NonNullable<T> {
if (x == null) {
throw new TypeError(
`throwIfNullOrUndefined: ${descriptionOfIt} was unexpectedly ${JSON.stringify(x)}.`,
);
}
return x;
}
type DescriptionOrRedundanceHint<T> = null extends T
? string
: undefined extends T
? string
: `It cannot be null or undefined; this check is redundant.`;