0
# Node.js Object Type Checking
1
2
Specialized type checking for Node.js specific objects including buffers and streams with support for readable, writable, and duplex streams.
3
4
## Capabilities
5
6
### Buffer Type Checking
7
8
Returns true if value is a Buffer instance.
9
10
```typescript { .api }
11
/**
12
* Returns true if val is buffer
13
* @param val - Value to check
14
* @returns Type guard indicating if value is Buffer
15
*/
16
function isBuffer(val: unknown): val is Buffer;
17
```
18
19
**Usage Example:**
20
21
```typescript
22
import { isBuffer } from "is-type-of";
23
24
const buf = Buffer.from("hello");
25
const arr = new Uint8Array([1, 2, 3]);
26
27
isBuffer(buf); // => true
28
isBuffer(arr); // => false
29
isBuffer("hello"); // => false
30
```
31
32
### Stream Type Checking
33
34
Returns true if value is a Stream instance.
35
36
```typescript { .api }
37
/**
38
* Returns true if val is stream
39
* @param val - Value to check
40
* @returns Type guard indicating if value is Stream
41
*/
42
function isStream(val?: unknown): val is Stream;
43
```
44
45
**Usage Example:**
46
47
```typescript
48
import { isStream } from "is-type-of";
49
import { Readable } from "node:stream";
50
51
const readable = new Readable();
52
53
isStream(readable); // => true
54
isStream({}); // => false
55
```
56
57
### Readable Stream Type Checking
58
59
Returns true if value is a readable stream.
60
61
```typescript { .api }
62
/**
63
* Returns true if val is readable stream
64
* @param val - Value to check
65
* @returns Type guard indicating if value is Readable
66
*/
67
function isReadable(val?: unknown): val is Readable;
68
```
69
70
**Usage Example:**
71
72
```typescript
73
import { isReadable } from "is-type-of";
74
import { Readable, Writable } from "node:stream";
75
76
const readable = new Readable();
77
const writable = new Writable();
78
79
isReadable(readable); // => true
80
isReadable(writable); // => false
81
```
82
83
### Writable Stream Type Checking
84
85
Returns true if value is a writable stream.
86
87
```typescript { .api }
88
/**
89
* Returns true if val is write stream
90
* @param val - Value to check
91
* @returns Type guard indicating if value is Writable
92
*/
93
function isWritable(val?: unknown): val is Writable;
94
```
95
96
**Usage Example:**
97
98
```typescript
99
import { isWritable } from "is-type-of";
100
import { Readable, Writable } from "node:stream";
101
102
const readable = new Readable();
103
const writable = new Writable();
104
105
isWritable(writable); // => true
106
isWritable(readable); // => false
107
```
108
109
### Duplex Stream Type Checking
110
111
Returns true if value is a duplex stream (both readable and writable).
112
113
```typescript { .api }
114
/**
115
* Returns true if val is duplex stream
116
* @param val - Value to check
117
* @returns Type guard indicating if value is Duplex
118
*/
119
function isDuplex(val?: unknown): val is Duplex;
120
```
121
122
**Usage Example:**
123
124
```typescript
125
import { isDuplex } from "is-type-of";
126
import { Duplex, Readable } from "node:stream";
127
128
const duplex = new Duplex();
129
const readable = new Readable();
130
131
isDuplex(duplex); // => true
132
isDuplex(readable); // => false
133
```
134
135
## Stream Type Hierarchy
136
137
Node.js streams have a specific inheritance hierarchy:
138
139
- **Stream**: Base class for all streams
140
- **Readable**: Streams you can read from
141
- **Writable**: Streams you can write to
142
- **Duplex**: Streams that are both readable and writable
143
144
The type checking functions respect this hierarchy:
145
146
```typescript
147
import { isStream, isReadable, isDuplex } from "is-type-of";
148
import { Duplex } from "node:stream";
149
150
const duplex = new Duplex();
151
152
isStream(duplex); // => true (duplex is a stream)
153
isReadable(duplex); // => true (duplex is readable)
154
isDuplex(duplex); // => true (duplex is duplex)
155
```
156
157
## Common Use Cases
158
159
### Processing Different Stream Types
160
161
```typescript
162
import { isReadable, isWritable, isDuplex } from "is-type-of";
163
164
function processStream(stream: unknown) {
165
if (isDuplex(stream)) {
166
console.log("Can both read from and write to this stream");
167
// stream is typed as Duplex
168
} else if (isReadable(stream)) {
169
console.log("Can read from this stream");
170
// stream is typed as Readable
171
} else if (isWritable(stream)) {
172
console.log("Can write to this stream");
173
// stream is typed as Writable
174
}
175
}
176
```
177
178
### Buffer vs Typed Array Detection
179
180
```typescript
181
import { isBuffer } from "is-type-of";
182
183
function processBufferLike(data: unknown) {
184
if (isBuffer(data)) {
185
// Node.js Buffer - has additional methods
186
console.log(data.toString('hex'));
187
} else if (data instanceof Uint8Array) {
188
// Browser-compatible typed array
189
console.log(Array.from(data));
190
}
191
}
192
```