0
# Typed Arrays and Binary Data
1
2
Type checking for typed arrays, array buffers, and binary data structures including all variants of typed arrays and related buffer types.
3
4
## Capabilities
5
6
### General Typed Array Checking
7
8
Check if a value is any type of typed array.
9
10
```typescript { .api }
11
/**
12
* Check if value is any typed array
13
* @param value - Value to check
14
* @returns True if value is a typed array
15
*/
16
function isTypedArray(value: unknown): value is TypedArray;
17
18
type TypedArray =
19
| Int8Array
20
| Uint8Array
21
| Uint8ClampedArray
22
| Int16Array
23
| Uint16Array
24
| Int32Array
25
| Uint32Array
26
| Float32Array
27
| Float64Array
28
| BigInt64Array
29
| BigUint64Array;
30
```
31
32
**Usage Examples:**
33
34
```typescript
35
import is from '@sindresorhus/is';
36
37
is.typedArray(new Uint8Array()); // => true
38
is.typedArray(new Float32Array()); // => true
39
is.typedArray(new BigInt64Array()); // => true
40
is.typedArray([]); // => false
41
is.typedArray(new ArrayBuffer(8)); // => false
42
43
// Type guard usage
44
function processTypedArray(value: unknown) {
45
if (is.typedArray(value)) {
46
// value is now typed as TypedArray
47
console.log('Length:', value.length);
48
console.log('Byte length:', value.byteLength);
49
console.log('Buffer:', value.buffer);
50
}
51
}
52
```
53
54
### 8-bit Integer Arrays
55
56
Type checking for 8-bit integer typed arrays.
57
58
```typescript { .api }
59
/**
60
* Check if value is Int8Array
61
* @param value - Value to check
62
* @returns True if value is Int8Array
63
*/
64
function isInt8Array(value: unknown): value is Int8Array;
65
66
/**
67
* Check if value is Uint8Array
68
* @param value - Value to check
69
* @returns True if value is Uint8Array
70
*/
71
function isUint8Array(value: unknown): value is Uint8Array;
72
73
/**
74
* Check if value is Uint8ClampedArray
75
* @param value - Value to check
76
* @returns True if value is Uint8ClampedArray
77
*/
78
function isUint8ClampedArray(value: unknown): value is Uint8ClampedArray;
79
```
80
81
**Usage Examples:**
82
83
```typescript
84
import is from '@sindresorhus/is';
85
86
// Int8Array: -128 to 127
87
const int8 = new Int8Array([127, -128]);
88
is.int8Array(int8); // => true
89
90
// Uint8Array: 0 to 255
91
const uint8 = new Uint8Array([0, 255]);
92
is.uint8Array(uint8); // => true
93
94
// Uint8ClampedArray: 0 to 255 (clamped)
95
const clamped = new Uint8ClampedArray([0, 255, 300]); // 300 becomes 255
96
is.uint8ClampedArray(clamped); // => true
97
98
// Cross-type checking
99
is.uint8Array(int8); // => false
100
is.int8Array(uint8); // => false
101
```
102
103
### 16-bit Integer Arrays
104
105
Type checking for 16-bit integer typed arrays.
106
107
```typescript { .api }
108
/**
109
* Check if value is Int16Array
110
* @param value - Value to check
111
* @returns True if value is Int16Array
112
*/
113
function isInt16Array(value: unknown): value is Int16Array;
114
115
/**
116
* Check if value is Uint16Array
117
* @param value - Value to check
118
* @returns True if value is Uint16Array
119
*/
120
function isUint16Array(value: unknown): value is Uint16Array;
121
```
122
123
**Usage Examples:**
124
125
```typescript
126
import is from '@sindresorhus/is';
127
128
// Int16Array: -32,768 to 32,767
129
const int16 = new Int16Array([32767, -32768]);
130
is.int16Array(int16); // => true
131
132
// Uint16Array: 0 to 65,535
133
const uint16 = new Uint16Array([0, 65535]);
134
is.uint16Array(uint16); // => true
135
136
// Working with binary data
137
function processAudioSamples(data: unknown) {
138
if (is.int16Array(data)) {
139
// data is now typed as Int16Array
140
for (let i = 0; i < data.length; i++) {
141
// Process 16-bit audio sample
142
const sample = data[i] / 32767; // Normalize to -1 to 1
143
console.log(sample);
144
}
145
}
146
}
147
```
148
149
### 32-bit Integer Arrays
150
151
Type checking for 32-bit integer typed arrays.
152
153
```typescript { .api }
154
/**
155
* Check if value is Int32Array
156
* @param value - Value to check
157
* @returns True if value is Int32Array
158
*/
159
function isInt32Array(value: unknown): value is Int32Array;
160
161
/**
162
* Check if value is Uint32Array
163
* @param value - Value to check
164
* @returns True if value is Uint32Array
165
*/
166
function isUint32Array(value: unknown): value is Uint32Array;
167
```
168
169
**Usage Examples:**
170
171
```typescript
172
import is from '@sindresorhus/is';
173
174
// Int32Array: -2,147,483,648 to 2,147,483,647
175
const int32 = new Int32Array([2147483647, -2147483648]);
176
is.int32Array(int32); // => true
177
178
// Uint32Array: 0 to 4,294,967,295
179
const uint32 = new Uint32Array([0, 4294967295]);
180
is.uint32Array(uint32); // => true
181
182
// Type guard usage
183
function processPixelData(data: unknown) {
184
if (is.uint32Array(data)) {
185
// data is now typed as Uint32Array
186
for (let i = 0; i < data.length; i++) {
187
const pixel = data[i];
188
const r = (pixel >> 24) & 0xFF;
189
const g = (pixel >> 16) & 0xFF;
190
const b = (pixel >> 8) & 0xFF;
191
const a = pixel & 0xFF;
192
console.log(`RGBA: ${r}, ${g}, ${b}, ${a}`);
193
}
194
}
195
}
196
```
197
198
### Floating Point Arrays
199
200
Type checking for floating-point typed arrays.
201
202
```typescript { .api }
203
/**
204
* Check if value is Float32Array
205
* @param value - Value to check
206
* @returns True if value is Float32Array
207
*/
208
function isFloat32Array(value: unknown): value is Float32Array;
209
210
/**
211
* Check if value is Float64Array
212
* @param value - Value to check
213
* @returns True if value is Float64Array
214
*/
215
function isFloat64Array(value: unknown): value is Float64Array;
216
```
217
218
**Usage Examples:**
219
220
```typescript
221
import is from '@sindresorhus/is';
222
223
// Float32Array: single precision floating point
224
const float32 = new Float32Array([3.14, -2.71]);
225
is.float32Array(float32); // => true
226
227
// Float64Array: double precision floating point
228
const float64 = new Float64Array([Math.PI, Math.E]);
229
is.float64Array(float64); // => true
230
231
// Working with scientific data
232
function processData(data: unknown) {
233
if (is.float64Array(data)) {
234
// data is now typed as Float64Array
235
const sum = data.reduce((acc, val) => acc + val, 0);
236
const average = sum / data.length;
237
console.log('Average:', average);
238
}
239
}
240
```
241
242
### BigInt Arrays
243
244
Type checking for BigInt typed arrays.
245
246
```typescript { .api }
247
/**
248
* Check if value is BigInt64Array
249
* @param value - Value to check
250
* @returns True if value is BigInt64Array
251
*/
252
function isBigInt64Array(value: unknown): value is BigInt64Array;
253
254
/**
255
* Check if value is BigUint64Array
256
* @param value - Value to check
257
* @returns True if value is BigUint64Array
258
*/
259
function isBigUint64Array(value: unknown): value is BigUint64Array;
260
```
261
262
**Usage Examples:**
263
264
```typescript
265
import is from '@sindresorhus/is';
266
267
// BigInt64Array: signed 64-bit integers
268
const bigInt64 = new BigInt64Array([123n, -456n]);
269
is.bigInt64Array(bigInt64); // => true
270
271
// BigUint64Array: unsigned 64-bit integers
272
const bigUint64 = new BigUint64Array([123n, 456n]);
273
is.bigUint64Array(bigUint64); // => true
274
275
// Working with large integers
276
function processLargeNumbers(data: unknown) {
277
if (is.bigInt64Array(data)) {
278
// data is now typed as BigInt64Array
279
for (const bigNum of data) {
280
console.log('Large number:', bigNum.toString());
281
}
282
}
283
}
284
```
285
286
### Array Buffer Types
287
288
Type checking for buffer objects that typed arrays are built on.
289
290
```typescript { .api }
291
/**
292
* Check if value is ArrayBuffer
293
* @param value - Value to check
294
* @returns True if value is ArrayBuffer
295
*/
296
function isArrayBuffer(value: unknown): value is ArrayBuffer;
297
298
/**
299
* Check if value is SharedArrayBuffer
300
* @param value - Value to check
301
* @returns True if value is SharedArrayBuffer
302
*/
303
function isSharedArrayBuffer(value: unknown): value is SharedArrayBuffer;
304
305
/**
306
* Check if value is DataView
307
* @param value - Value to check
308
* @returns True if value is DataView
309
*/
310
function isDataView(value: unknown): value is DataView;
311
```
312
313
**Usage Examples:**
314
315
```typescript
316
import is from '@sindresorhus/is';
317
318
// ArrayBuffer: raw binary data buffer
319
const buffer = new ArrayBuffer(16);
320
is.arrayBuffer(buffer); // => true
321
322
// SharedArrayBuffer: shared between workers
323
const sharedBuffer = new SharedArrayBuffer(1024);
324
is.sharedArrayBuffer(sharedBuffer); // => true
325
326
// DataView: flexible view of buffer
327
const view = new DataView(buffer);
328
is.dataView(view); // => true
329
330
// Working with buffers
331
function processBuffer(data: unknown) {
332
if (is.arrayBuffer(data)) {
333
// data is now typed as ArrayBuffer
334
console.log('Buffer size:', data.byteLength);
335
336
// Create typed array views
337
const uint8View = new Uint8Array(data);
338
const uint32View = new Uint32Array(data);
339
340
console.log('As Uint8Array length:', uint8View.length);
341
console.log('As Uint32Array length:', uint32View.length);
342
}
343
}
344
345
function processDataView(view: unknown) {
346
if (is.dataView(view)) {
347
// view is now typed as DataView
348
const int32 = view.getInt32(0, true); // little endian
349
const float64 = view.getFloat64(8, false); // big endian
350
console.log('Int32:', int32, 'Float64:', float64);
351
}
352
}
353
```
354
355
### Node.js Buffer Type Checking
356
357
Check if a value is a Node.js Buffer (with deprecation note).
358
359
```typescript { .api }
360
/**
361
* Check if value is Node.js Buffer
362
* Note: Prefer using Uint8Array instead of Buffer
363
* @param value - Value to check
364
* @returns True if value is Buffer
365
*/
366
function isBuffer(value: unknown): value is NodeBuffer;
367
368
type NodeBuffer = Buffer; // Node.js Buffer type
369
```
370
371
**Usage Examples:**
372
373
```typescript
374
import is from '@sindresorhus/is';
375
376
// Node.js only
377
const buffer = Buffer.from('hello');
378
is.buffer(buffer); // => true
379
380
const uint8Array = new Uint8Array([1, 2, 3]);
381
is.buffer(uint8Array); // => false
382
383
// Migration pattern
384
function processData(data: unknown) {
385
if (is.buffer(data)) {
386
// Convert Buffer to Uint8Array (recommended)
387
const uint8Array = new Uint8Array(data);
388
return uint8Array;
389
} else if (is.uint8Array(data)) {
390
return data;
391
}
392
throw new Error('Expected Buffer or Uint8Array');
393
}
394
```
395
396
## Usage Patterns
397
398
### Binary Data Processing
399
400
```typescript
401
import is from '@sindresorhus/is';
402
403
function processBinaryData(data: unknown) {
404
if (is.uint8Array(data)) {
405
// Process as byte array
406
for (let i = 0; i < data.length; i++) {
407
console.log(`Byte ${i}: ${data[i]}`);
408
}
409
} else if (is.float32Array(data)) {
410
// Process as floating point data
411
const max = Math.max(...data);
412
const min = Math.min(...data);
413
console.log(`Range: ${min} to ${max}`);
414
} else if (is.arrayBuffer(data)) {
415
// Create appropriate view
416
const view = new Uint8Array(data);
417
processBinaryData(view);
418
}
419
}
420
```
421
422
### Image Data Processing
423
424
```typescript
425
import is from '@sindresorhus/is';
426
427
function processImageData(data: unknown) {
428
if (is.uint8ClampedArray(data)) {
429
// Canvas ImageData uses Uint8ClampedArray
430
for (let i = 0; i < data.length; i += 4) {
431
const r = data[i];
432
const g = data[i + 1];
433
const b = data[i + 2];
434
const a = data[i + 3];
435
436
// Process RGBA pixel
437
const gray = Math.round(0.299 * r + 0.587 * g + 0.114 * b);
438
data[i] = data[i + 1] = data[i + 2] = gray;
439
}
440
}
441
}
442
```
443
444
## Notes
445
446
- Typed arrays provide efficient storage and manipulation of binary data
447
- All typed arrays share common properties: `length`, `byteLength`, `buffer`, `byteOffset`
448
- `Uint8ClampedArray` automatically clamps values to 0-255 range
449
- BigInt arrays work with `bigint` primitives, not regular numbers
450
- `SharedArrayBuffer` enables sharing memory between web workers
451
- `DataView` provides flexible, endian-aware access to ArrayBuffer data
452
- Node.js `Buffer` is being deprecated in favor of `Uint8Array`
453
- All typed array checks work with TypeScript type guards for compile-time type narrowing