-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathis-empty.ts
More file actions
29 lines (23 loc) · 612 Bytes
/
is-empty.ts
File metadata and controls
29 lines (23 loc) · 612 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
25
26
27
28
29
/**
Check whether an array is empty.
This is useful because doing `array.length === 0` on its own won't work as a type-guard.
@example
```
import {isEmpty} from 'ts-extras';
isEmpty([1, 2, 3]);
//=> false
isEmpty([]);
//=> true
// Works with tuples
const tuple: [string, number] | [] = Math.random() > 0.5 ? ['hello', 42] : [];
if (isEmpty(tuple)) {
// tuple is now typed as []
} else {
// tuple is now typed as [string, number]
}
```
@category Type guard
*/
export function isEmpty<T extends readonly unknown[]>(array: T): array is T extends readonly never[] ? T : never {
return array.length === 0;
}