-
Notifications
You must be signed in to change notification settings - Fork 0
/
guards.ts
26 lines (22 loc) · 828 Bytes
/
guards.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
type PrimitiveValue = string | number | boolean | bigint;
export function isPrimitiveValue(value: unknown): value is PrimitiveValue {
return typeof value === "string" || typeof value === "number" ||
typeof value === "boolean" || typeof value === "bigint";
}
export function isPromiseLike<T>(
value: T | PromiseLike<T> | unknown,
): value is PromiseLike<T> {
return typeof (value as PromiseLike<T>)?.then === "function";
}
export function isIterable<T>(
value: unknown | Iterable<T>,
): value is Iterable<T> {
return typeof value !== "string" &&
typeof (value as Iterable<T>)?.[Symbol.iterator] === "function";
}
export function isAsyncIterable<T>(
value: unknown | AsyncIterable<T>,
): value is AsyncIterable<T> {
return typeof (value as AsyncIterable<T>)?.[Symbol.asyncIterator] ===
"function";
}