0
# Type Checking
1
2
Comprehensive type validation system with both basic JavaScript type checking and advanced type detection for modern JavaScript features.
3
4
## Capabilities
5
6
### Basic Type Checking
7
8
Fundamental type checking functions for JavaScript primitives and common objects.
9
10
```javascript { .api }
11
/**
12
* Checks if value is an array
13
* @param {any} ar - Value to check
14
* @returns {boolean} True if value is an array
15
*/
16
function isArray(ar): boolean;
17
18
/**
19
* Checks if value is a boolean
20
* @param {any} arg - Value to check
21
* @returns {boolean} True if value is a boolean
22
*/
23
function isBoolean(arg): boolean;
24
25
/**
26
* Checks if value is null
27
* @param {any} arg - Value to check
28
* @returns {boolean} True if value is null
29
*/
30
function isNull(arg): boolean;
31
32
/**
33
* Checks if value is null or undefined
34
* @param {any} arg - Value to check
35
* @returns {boolean} True if value is null or undefined
36
*/
37
function isNullOrUndefined(arg): boolean;
38
39
/**
40
* Checks if value is a number
41
* @param {any} arg - Value to check
42
* @returns {boolean} True if value is a number
43
*/
44
function isNumber(arg): boolean;
45
46
/**
47
* Checks if value is a string
48
* @param {any} arg - Value to check
49
* @returns {boolean} True if value is a string
50
*/
51
function isString(arg): boolean;
52
53
/**
54
* Checks if value is a symbol
55
* @param {any} arg - Value to check
56
* @returns {boolean} True if value is a symbol
57
*/
58
function isSymbol(arg): boolean;
59
60
/**
61
* Checks if value is undefined
62
* @param {any} arg - Value to check
63
* @returns {boolean} True if value is undefined
64
*/
65
function isUndefined(arg): boolean;
66
67
/**
68
* Checks if value is a regular expression
69
* @param {any} re - Value to check
70
* @returns {boolean} True if value is a RegExp
71
*/
72
function isRegExp(re): boolean;
73
74
/**
75
* Checks if value is an object (not null)
76
* @param {any} arg - Value to check
77
* @returns {boolean} True if value is an object and not null
78
*/
79
function isObject(arg): boolean;
80
81
/**
82
* Checks if value is a Date object
83
* @param {any} d - Value to check
84
* @returns {boolean} True if value is a Date
85
*/
86
function isDate(d): boolean;
87
88
/**
89
* Checks if value is an Error object
90
* @param {any} e - Value to check
91
* @returns {boolean} True if value is an Error
92
*/
93
function isError(e): boolean;
94
95
/**
96
* Checks if value is a function
97
* @param {any} arg - Value to check
98
* @returns {boolean} True if value is a function
99
*/
100
function isFunction(arg): boolean;
101
102
/**
103
* Checks if value is a primitive type
104
* @param {any} arg - Value to check
105
* @returns {boolean} True if value is null, boolean, number, string, symbol, or undefined
106
*/
107
function isPrimitive(arg): boolean;
108
109
/**
110
* Checks if value is a Buffer (Node.js/browser compatible)
111
* @param {any} arg - Value to check
112
* @returns {boolean} True if value is a Buffer
113
*/
114
function isBuffer(arg): boolean;
115
```
116
117
**Usage Examples:**
118
119
```javascript
120
const util = require('util');
121
122
// Basic type checks
123
util.isString('hello'); // true
124
util.isNumber(42); // true
125
util.isArray([1, 2, 3]); // true
126
util.isFunction(() => {}); // true
127
util.isObject({}); // true
128
util.isNull(null); // true
129
util.isUndefined(undefined); // true
130
131
// Date and RegExp
132
util.isDate(new Date()); // true
133
util.isRegExp(/pattern/); // true
134
135
// Error checking
136
util.isError(new Error('message')); // true
137
util.isError(new TypeError('type error')); // true
138
139
// Primitives
140
util.isPrimitive('string'); // true
141
util.isPrimitive(42); // true
142
util.isPrimitive({}); // false
143
```
144
145
### Extended Type Checking (util.types)
146
147
Advanced type detection for modern JavaScript features including collections, typed arrays, and boxed primitives.
148
149
```javascript { .api }
150
const types: {
151
// Collection Types
152
isMap(value): boolean;
153
isSet(value): boolean;
154
isWeakMap(value): boolean;
155
isWeakSet(value): boolean;
156
157
// Array Buffer Types
158
isArrayBuffer(value): boolean;
159
isSharedArrayBuffer(value): boolean;
160
isArrayBufferView(value): boolean;
161
isDataView(value): boolean;
162
isAnyArrayBuffer(value): boolean;
163
164
// Typed Arrays
165
isTypedArray(value): boolean;
166
isUint8Array(value): boolean;
167
isUint8ClampedArray(value): boolean;
168
isUint16Array(value): boolean;
169
isUint32Array(value): boolean;
170
isInt8Array(value): boolean;
171
isInt16Array(value): boolean;
172
isInt32Array(value): boolean;
173
isFloat32Array(value): boolean;
174
isFloat64Array(value): boolean;
175
isBigInt64Array(value): boolean;
176
isBigUint64Array(value): boolean;
177
178
// Boxed Primitives
179
isNumberObject(value): boolean;
180
isStringObject(value): boolean;
181
isBooleanObject(value): boolean;
182
isBigIntObject(value): boolean;
183
isSymbolObject(value): boolean;
184
isBoxedPrimitive(value): boolean;
185
186
// Function Types
187
isGeneratorFunction(value): boolean;
188
isAsyncFunction(value): boolean;
189
isGeneratorObject(value): boolean;
190
191
// Iterator Types
192
isMapIterator(value): boolean;
193
isSetIterator(value): boolean;
194
195
// Other Types
196
isArgumentsObject(value): boolean;
197
isPromise(value): boolean;
198
isWebAssemblyCompiledModule(value): boolean;
199
200
// Aliases
201
isDate(value): boolean;
202
isRegExp(value): boolean;
203
isNativeError(value): boolean;
204
205
// Unsupported (throw errors)
206
isProxy(): never;
207
isExternal(): never;
208
isModuleNamespaceObject(): never;
209
};
210
```
211
212
**Usage Examples:**
213
214
```javascript
215
const util = require('util');
216
217
// Collections
218
util.types.isMap(new Map()); // true
219
util.types.isSet(new Set()); // true
220
util.types.isWeakMap(new WeakMap()); // true
221
222
// Typed Arrays
223
util.types.isUint8Array(new Uint8Array()); // true
224
util.types.isFloat32Array(new Float32Array()); // true
225
util.types.isTypedArray(new Int16Array()); // true
226
227
// Array Buffers
228
util.types.isArrayBuffer(new ArrayBuffer(16)); // true
229
util.types.isDataView(new DataView(new ArrayBuffer(16))); // true
230
231
// Boxed Primitives
232
util.types.isNumberObject(new Number(42)); // true
233
util.types.isStringObject(new String('hello')); // true
234
util.types.isBoxedPrimitive(new Boolean(true)); // true
235
236
// Functions
237
util.types.isAsyncFunction(async () => {}); // true
238
util.types.isGeneratorFunction(function* () {}); // true
239
240
// Promises
241
util.types.isPromise(Promise.resolve()); // true
242
util.types.isPromise({ then: () => {}, catch: () => {} }); // true
243
244
// Arguments object
245
function example() {
246
return util.types.isArgumentsObject(arguments); // true
247
}
248
```