0
# Array Type Checking
1
2
Array validation functions providing comprehensive array detection including empty/full array checks and general array validation.
3
4
## Capabilities
5
6
### Array Type Checking
7
8
Returns whether the payload is an array.
9
10
```typescript { .api }
11
/**
12
* Returns whether the payload is an array
13
* @param payload - Value to check
14
* @returns Type guard indicating if payload is array
15
*/
16
function isArray(payload: unknown): payload is unknown[];
17
```
18
19
**Usage Examples:**
20
21
```typescript
22
import { isArray } from "is-what";
23
24
isArray([]); // true
25
isArray([1, 2, 3]); // true
26
isArray(["a", "b", "c"]); // true
27
isArray(new Array()); // true
28
isArray({}); // false
29
isArray("[]"); // false
30
isArray(null); // false
31
32
if (isArray(data)) {
33
// TypeScript knows data is an array
34
console.log(`Array has ${data.length} items`);
35
data.forEach(item => console.log(item));
36
}
37
```
38
39
### Empty Array Checking
40
41
Returns whether the payload is an empty array (length === 0).
42
43
```typescript { .api }
44
/**
45
* Returns whether the payload is an empty array
46
* @param payload - Value to check
47
* @returns Type guard indicating if payload is empty array
48
*/
49
function isEmptyArray(payload: unknown): payload is [];
50
```
51
52
**Usage Examples:**
53
54
```typescript
55
import { isEmptyArray } from "is-what";
56
57
isEmptyArray([]); // true
58
isEmptyArray([1]); // false
59
isEmptyArray([null]); // false
60
isEmptyArray({}); // false
61
isEmptyArray(""); // false
62
63
// Useful for validation
64
function processData(input: unknown) {
65
if (isEmptyArray(input)) {
66
console.log("No data to process");
67
return;
68
}
69
70
// Continue processing...
71
}
72
73
// Type narrowing example
74
if (isEmptyArray(list)) {
75
// TypeScript knows list is [] (empty array)
76
console.log("Empty array detected");
77
} else if (isArray(list)) {
78
// TypeScript knows list is unknown[] with length > 0
79
console.log(`Processing ${list.length} items`);
80
}
81
```
82
83
### Full Array Checking
84
85
Returns whether the payload is a non-empty array (has at least one element).
86
87
```typescript { .api }
88
/**
89
* Returns whether the payload is an array with at least one element
90
* @param payload - Value to check
91
* @returns Type guard indicating if payload is non-empty array
92
*/
93
function isFullArray(payload: unknown): payload is unknown[];
94
```
95
96
**Usage Examples:**
97
98
```typescript
99
import { isFullArray } from "is-what";
100
101
isFullArray([1, 2, 3]); // true
102
isFullArray(["item"]); // true
103
isFullArray([null]); // true (contains one element, even if null)
104
isFullArray([]); // false
105
isFullArray("full"); // false
106
isFullArray({}); // false
107
108
// Practical usage
109
function getFirstItem(input: unknown) {
110
if (isFullArray(input)) {
111
return input[0]; // Safe to access first element
112
}
113
return null;
114
}
115
116
// Validation pattern
117
function validateRequiredList(list: unknown, fieldName: string) {
118
if (!isFullArray(list)) {
119
throw new Error(`${fieldName} must be a non-empty array`);
120
}
121
122
// TypeScript knows list is unknown[] with at least one element
123
return list;
124
}
125
```
126
127
## Combined Array Validation Patterns
128
129
```typescript
130
import { isArray, isEmptyArray, isFullArray } from "is-what";
131
132
function analyzeArray(value: unknown) {
133
if (!isArray(value)) {
134
return "Not an array";
135
}
136
137
if (isEmptyArray(value)) {
138
return "Empty array";
139
}
140
141
if (isFullArray(value)) {
142
return `Array with ${value.length} items`;
143
}
144
145
// This shouldn't happen, but TypeScript doesn't know that
146
return "Unknown array state";
147
}
148
149
// Array processing pipeline
150
function processArrayData(input: unknown) {
151
// First check if it's an array at all
152
if (!isArray(input)) {
153
throw new Error("Input must be an array");
154
}
155
156
// Handle empty array case
157
if (isEmptyArray(input)) {
158
console.log("No items to process");
159
return [];
160
}
161
162
// Process non-empty array
163
console.log(`Processing ${input.length} items`);
164
return input.map((item, index) => ({
165
index,
166
value: item,
167
processed: true
168
}));
169
}
170
171
// Type-safe array operations
172
function safeArrayOperations(data: unknown) {
173
if (isArray(data)) {
174
// All array methods are available
175
const doubled = data.map(x => x);
176
const filtered = data.filter(x => x != null);
177
178
if (isEmptyArray(data)) {
179
console.log("Cannot perform operations on empty array");
180
return null;
181
}
182
183
if (isFullArray(data)) {
184
// Safe to access elements
185
const first = data[0];
186
const last = data[data.length - 1];
187
return { first, last, length: data.length };
188
}
189
}
190
191
return null;
192
}
193
194
// Example usage
195
analyzeArray([]); // "Empty array"
196
analyzeArray([1, 2, 3]); // "Array with 3 items"
197
analyzeArray("not array"); // "Not an array"
198
199
const result = processArrayData([1, 2, 3]);
200
// Logs: "Processing 3 items"
201
// Returns: [
202
// { index: 0, value: 1, processed: true },
203
// { index: 1, value: 2, processed: true },
204
// { index: 2, value: 3, processed: true }
205
// ]
206
```