Complete type checking utility library for Node.js with TypeScript support and type guards
npx @tessl/cli install tessl/npm-is-type-of@2.2.00
# is-type-of
1
2
is-type-of is a comprehensive type checking utility library for Node.js that provides complete type validation including primitive types, standard objects, and Node.js specific objects. It offers both ES Module and CommonJS compatibility with full TypeScript support including type guards for enhanced type safety in TypeScript applications.
3
4
## Package Information
5
6
- **Package Name**: is-type-of
7
- **Package Type**: npm
8
- **Language**: TypeScript
9
- **Installation**: `npm install is-type-of`
10
11
## Core Imports
12
13
```typescript
14
import is from "is-type-of";
15
import { isArray, isString, isNumber } from "is-type-of";
16
```
17
18
For CommonJS:
19
20
```javascript
21
const is = require("is-type-of");
22
const { isArray, isString } = require("is-type-of");
23
```
24
25
## Basic Usage
26
27
```typescript
28
import is from "is-type-of";
29
import { isArray, isString } from "is-type-of";
30
31
// Using the main is object
32
is.array([1, 2, 3]); // => true
33
is.string("hello"); // => true
34
is.primitive(42); // => true
35
is.primitive({}); // => false
36
37
// Using named imports
38
isArray([1, 2, 3]); // => true
39
isString("hello"); // => true
40
41
// Type guard usage in TypeScript
42
function processValue(value: string | number[]) {
43
if (isArray(value)) {
44
// value is now typed as number[]
45
value.forEach(console.log);
46
} else {
47
// value is now typed as string
48
console.log(value.toUpperCase());
49
}
50
}
51
```
52
53
## Architecture
54
55
is-type-of is organized around several key concepts:
56
57
- **Central `is` Object**: Main namespace containing all type checking methods organized by category
58
- **Named Exports**: Individual functions for direct imports and tree-shaking
59
- **Type Guards**: All functions act as TypeScript type guards for compile-time type safety
60
- **Legacy Compatibility**: Deprecated exports maintain backward compatibility
61
- **Comprehensive Coverage**: Support for primitives, standard objects, Node.js objects, and external libraries
62
63
## Capabilities
64
65
### Primitive Type Checking
66
67
Complete type checking for JavaScript primitive types including strings, numbers, booleans, symbols, null, undefined, and bigint. Includes specialized number checkers for integers, safe integers, and doubles.
68
69
```typescript { .api }
70
// Core primitive checkers
71
function isString(val?: unknown): val is string;
72
function isNumber(val?: unknown): val is number;
73
function isBoolean(val?: unknown): val is boolean;
74
function isSymbol(val?: unknown): val is symbol;
75
function isUndefined(val?: unknown): val is undefined;
76
function isNull(val?: unknown): val is null;
77
function isBigInt(val?: unknown): val is bigint;
78
79
// Composite primitive checkers
80
function isNullable(val?: unknown): val is null | undefined;
81
function isPrimitive(val?: unknown): val is Primitive;
82
83
// Number specializations
84
function isInteger(val?: unknown): val is number;
85
function isInteger32(val?: unknown): val is number;
86
function isLong(val?: unknown): val is number;
87
function isSafeInteger(val?: unknown): val is number;
88
function isDouble(val?: unknown): val is number;
89
function isNaN(val?: unknown): boolean;
90
function isFinite(val?: unknown): val is number;
91
```
92
93
[Primitive Types](./primitive-types.md)
94
95
### Standard Object Type Checking
96
97
Type checking for standard JavaScript objects including arrays, functions, classes, dates, errors, regular expressions, generators, and promises.
98
99
```typescript { .api }
100
// Core object checkers
101
function isArray<T = any>(val?: unknown): val is Array<T>;
102
function isFunction<T extends Function>(val?: unknown): val is T;
103
function isObject(val?: unknown): val is object;
104
function isDate(val?: unknown): val is Date;
105
function isError(val?: unknown): val is Error;
106
function isRegExp(val?: unknown): val is RegExp;
107
108
// Function specializations
109
function isGeneratorFunction(val?: unknown): val is GeneratorFunction;
110
function isAsyncFunction(val?: unknown): val is Function;
111
function isAsyncGeneratorFunction(val?: unknown): val is AsyncGeneratorFunction;
112
function isClass<T extends Class>(val?: unknown): val is T;
113
114
// Advanced objects
115
function isGenerator(val?: unknown): val is Generator;
116
function isPromise<T = any>(val?: unknown): val is Promise<T>;
117
function isPromiseLike<T = any>(val?: unknown): val is PromiseLike<T>;
118
```
119
120
[Standard Objects](./standard-objects.md)
121
122
### Node.js Object Type Checking
123
124
Specialized type checking for Node.js specific objects including buffers and streams with support for readable, writable, and duplex streams.
125
126
```typescript { .api }
127
function isBuffer(val: unknown): val is Buffer;
128
function isStream(val?: unknown): val is Stream;
129
function isReadable(val?: unknown): val is Readable;
130
function isWritable(val?: unknown): val is Writable;
131
function isDuplex(val?: unknown): val is Duplex;
132
```
133
134
[Node.js Objects](./node-objects.md)
135
136
### External Object Type Checking
137
138
Type checking for objects from external libraries, currently supporting Long objects from the 'long' npm package.
139
140
```typescript { .api }
141
function isLongObject(obj?: unknown): obj is LongObject;
142
143
interface LongObject {
144
high: number;
145
low: number;
146
}
147
```
148
149
[External Objects](./external-objects.md)
150
151
### Utility Functions
152
153
Type-safe utility functions for advanced type checking scenarios.
154
155
```typescript { .api }
156
/**
157
* Returns true if val is an instance of the given class constructor
158
* @param val - Value to check
159
* @param Clazz - Constructor function to check against
160
* @returns Type guard indicating if value is instance of Clazz
161
*/
162
function isInstanceOf<T extends Class>(val: unknown, Clazz: T): val is InstanceType<T>;
163
```
164
165
**Usage Example:**
166
167
```typescript
168
import { isInstanceOf } from "is-type-of";
169
170
class MyClass {
171
value: number;
172
constructor(value: number) {
173
this.value = value;
174
}
175
}
176
177
const instance = new MyClass(42);
178
const obj = { value: 42 };
179
180
isInstanceOf(instance, MyClass); // => true
181
isInstanceOf(obj, MyClass); // => false
182
183
// Type guard usage
184
function processValue(val: unknown) {
185
if (isInstanceOf(val, MyClass)) {
186
// val is now typed as MyClass
187
console.log(val.value);
188
}
189
}
190
```
191
192
## Central `is` Object
193
194
All type checking functions are available through the central `is` object:
195
196
```typescript { .api }
197
const is: {
198
// Primitive types
199
boolean: typeof isBoolean;
200
number: typeof isNumber;
201
string: typeof isString;
202
symbol: typeof isSymbol;
203
undefined: typeof isUndefined;
204
null: typeof isNull;
205
nullable: typeof isNullable;
206
bigInt: typeof isBigInt;
207
primitive: typeof isPrimitive;
208
integer: typeof isInteger;
209
integer32: typeof isInteger32;
210
long: typeof isLong;
211
double: typeof isDouble;
212
finite: typeof isFinite;
213
NaN: typeof isNaN;
214
safeInteger: typeof isSafeInteger;
215
216
// Standard objects
217
function: typeof isFunction;
218
generatorFunction: typeof isGeneratorFunction;
219
asyncFunction: typeof isAsyncFunction;
220
asyncGeneratorFunction: typeof isAsyncGeneratorFunction;
221
class: typeof isClass;
222
array: typeof isArray;
223
object: typeof isObject;
224
date: typeof isDate;
225
error: typeof isError;
226
regExp: typeof isRegExp;
227
generator: typeof isGenerator;
228
promise: typeof isPromise;
229
promiseLike: typeof isPromiseLike;
230
231
// Node.js objects
232
buffer: typeof isBuffer;
233
stream: typeof isStream;
234
readable: typeof isReadable;
235
writable: typeof isWritable;
236
duplex: typeof isDuplex;
237
238
// External objects
239
longObject: typeof isLongObject;
240
};
241
```
242
243
## Type Definitions
244
245
```typescript { .api }
246
type Primitive = string | number | bigint | boolean | symbol | null | undefined;
247
type Nullable = null | undefined;
248
type Class = new (...args: any[]) => any;
249
250
interface LongObject {
251
high: number;
252
low: number;
253
}
254
```