0
# Standard Object Type Checking
1
2
Type checking for standard JavaScript objects including arrays, functions, classes, dates, errors, regular expressions, generators, and promises.
3
4
## Capabilities
5
6
### Array Type Checking
7
8
Returns true if value is an array (doesn't check array items).
9
10
```typescript { .api }
11
/**
12
* Returns true if val is array, it won't check items of array
13
* @param val - Value to check
14
* @returns Type guard indicating if value is array
15
*/
16
function isArray<T = any>(val?: unknown): val is Array<T>;
17
```
18
19
**Usage Example:**
20
21
```typescript
22
import { isArray } from "is-type-of";
23
24
isArray([1, 2, 3]); // => true
25
isArray("hello"); // => false
26
isArray({length: 3}); // => false
27
28
// With type parameter
29
function processNumbers(value: unknown) {
30
if (isArray<number>(value)) {
31
// value is now typed as number[]
32
value.forEach(n => console.log(n * 2));
33
}
34
}
35
```
36
37
### Function Type Checking
38
39
Returns true if value is a function.
40
41
```typescript { .api }
42
/**
43
* Returns true if val is function
44
* @param val - Value to check
45
* @returns Type guard indicating if value is function
46
*/
47
function isFunction<T extends Function>(val?: unknown): val is T;
48
```
49
50
**Usage Example:**
51
52
```typescript
53
import { isFunction } from "is-type-of";
54
55
isFunction(() => {}); // => true
56
isFunction(function() {}); // => true
57
isFunction(Math.max); // => true
58
isFunction("function"); // => false
59
```
60
61
### Generator Function Type Checking
62
63
Returns true if value is a generator function.
64
65
```typescript { .api }
66
/**
67
* Returns true if val is generator function
68
* @param val - Value to check
69
* @returns Type guard indicating if value is generator function
70
*/
71
function isGeneratorFunction(val?: unknown): val is GeneratorFunction;
72
```
73
74
**Usage Example:**
75
76
```typescript
77
import { isGeneratorFunction } from "is-type-of";
78
79
function* generator() { yield 1; }
80
81
isGeneratorFunction(generator); // => true
82
isGeneratorFunction(() => {}); // => false
83
```
84
85
### Async Function Type Checking
86
87
Returns true if value is an async function.
88
89
```typescript { .api }
90
/**
91
* Returns true if val is async function
92
* @param val - Value to check
93
* @returns Type guard indicating if value is async function
94
*/
95
function isAsyncFunction(val?: unknown): val is Function;
96
```
97
98
**Usage Example:**
99
100
```typescript
101
import { isAsyncFunction } from "is-type-of";
102
103
async function asyncFn() { return 42; }
104
105
isAsyncFunction(asyncFn); // => true
106
isAsyncFunction(() => {}); // => false
107
```
108
109
### Async Generator Function Type Checking
110
111
Returns true if value is an async generator function.
112
113
```typescript { .api }
114
/**
115
* Returns true if val is async generator function
116
* @param val - Value to check
117
* @returns Type guard indicating if value is async generator function
118
*/
119
function isAsyncGeneratorFunction(val?: unknown): val is AsyncGeneratorFunction;
120
```
121
122
**Usage Example:**
123
124
```typescript
125
import { isAsyncGeneratorFunction } from "is-type-of";
126
127
async function* asyncGenerator() { yield 1; }
128
129
isAsyncGeneratorFunction(asyncGenerator); // => true
130
isAsyncGeneratorFunction(() => {}); // => false
131
```
132
133
### Object Type Checking
134
135
Returns true if value is an object (and not null).
136
137
```typescript { .api }
138
/**
139
* Returns true if val is object
140
* @param val - Value to check
141
* @returns Type guard indicating if value is object
142
*/
143
function isObject(val?: unknown): val is object;
144
```
145
146
**Usage Example:**
147
148
```typescript
149
import { isObject } from "is-type-of";
150
151
isObject({}); // => true
152
isObject([]); // => true
153
isObject(null); // => false
154
isObject("string"); // => false
155
```
156
157
### Class Type Checking
158
159
Returns true if value is a class constructor.
160
161
```typescript { .api }
162
/**
163
* Returns true if val is class
164
* Note: "class" is supported in ECMAScript 6, and if the code is using some compiler or transpiler, the checking might fail
165
* @param val - Value to check
166
* @returns Type guard indicating if value is class
167
*/
168
function isClass<T extends Class>(val?: unknown): val is T;
169
```
170
171
**Usage Example:**
172
173
```typescript
174
import { isClass } from "is-type-of";
175
176
class MyClass {}
177
178
isClass(MyClass); // => true
179
isClass(() => {}); // => false
180
```
181
182
### Regular Expression Type Checking
183
184
Returns true if value is a regular expression.
185
186
```typescript { .api }
187
/**
188
* Returns true if val is regular expression
189
* @param val - Value to check
190
* @returns Type guard indicating if value is RegExp
191
*/
192
function isRegExp(val?: unknown): val is RegExp;
193
```
194
195
**Usage Example:**
196
197
```typescript
198
import { isRegExp } from "is-type-of";
199
200
isRegExp(/pattern/); // => true
201
isRegExp(new RegExp("pattern")); // => true
202
isRegExp("pattern"); // => false
203
```
204
205
### Date Type Checking
206
207
Returns true if value is a Date instance.
208
209
```typescript { .api }
210
/**
211
* Returns true if val is instance of Date
212
* @param val - Value to check
213
* @returns Type guard indicating if value is Date
214
*/
215
function isDate(val?: unknown): val is Date;
216
```
217
218
**Usage Example:**
219
220
```typescript
221
import { isDate } from "is-type-of";
222
223
isDate(new Date()); // => true
224
isDate("2023-01-01"); // => false
225
isDate(Date.now()); // => false
226
```
227
228
### Error Type Checking
229
230
Returns true if value is an Error instance.
231
232
```typescript { .api }
233
/**
234
* Returns true if val is instance of Error
235
* @param val - Value to check
236
* @returns Type guard indicating if value is Error
237
*/
238
function isError(val?: unknown): val is Error;
239
```
240
241
**Usage Example:**
242
243
```typescript
244
import { isError } from "is-type-of";
245
246
isError(new Error("message")); // => true
247
isError(new TypeError("message")); // => true
248
isError("error message"); // => false
249
```
250
251
### Generator Type Checking
252
253
Returns true if value is a generator object.
254
255
```typescript { .api }
256
/**
257
* Returns true if val is generator
258
* @param val - Value to check
259
* @returns Type guard indicating if value is Generator
260
*/
261
function isGenerator(val?: unknown): val is Generator;
262
```
263
264
**Usage Example:**
265
266
```typescript
267
import { isGenerator } from "is-type-of";
268
269
function* generatorFn() { yield 1; }
270
const gen = generatorFn();
271
272
isGenerator(gen); // => true
273
isGenerator(generatorFn); // => false
274
```
275
276
### Promise Type Checking
277
278
Returns true if value is a Promise.
279
280
```typescript { .api }
281
/**
282
* Returns true if val is promise
283
* @param val - Value to check
284
* @returns Type guard indicating if value is Promise
285
*/
286
function isPromise<T = any>(val?: unknown): val is Promise<T>;
287
```
288
289
**Usage Example:**
290
291
```typescript
292
import { isPromise } from "is-type-of";
293
294
isPromise(Promise.resolve(42)); // => true
295
isPromise(new Promise(() => {})); // => true
296
isPromise({then: () => {}}); // => false (use isPromiseLike for this)
297
```
298
299
### Promise-like Type Checking
300
301
Returns true if value is promise-like (has a 'then' method).
302
303
```typescript { .api }
304
/**
305
* Returns true if val is like promise, if the object has then property, the checking will pass
306
* @param val - Value to check
307
* @returns Type guard indicating if value is PromiseLike
308
*/
309
function isPromiseLike<T = any>(val?: unknown): val is PromiseLike<T>;
310
```
311
312
**Usage Example:**
313
314
```typescript
315
import { isPromiseLike } from "is-type-of";
316
317
const thenable = {
318
then: (onResolve: Function) => onResolve(42)
319
};
320
321
isPromiseLike(Promise.resolve(42)); // => true
322
isPromiseLike(thenable); // => true
323
isPromiseLike({}); // => false
324
```
325
326
## Type Definitions
327
328
```typescript { .api }
329
type Class = new (...args: any[]) => any;
330
```