0
# Primitive Type Checking
1
2
Complete type checking for JavaScript primitive types including strings, numbers, booleans, symbols, null, undefined, and bigint. Also includes specialized number checkers for integers, safe integers, and doubles.
3
4
## Capabilities
5
6
### String Type Checking
7
8
Returns true if value is a string primitive (not String object).
9
10
```typescript { .api }
11
/**
12
* Returns true if val is string, not String object
13
* @param val - Value to check
14
* @returns Type guard indicating if value is string
15
*/
16
function isString(val?: unknown): val is string;
17
```
18
19
**Usage Example:**
20
21
```typescript
22
import { isString } from "is-type-of";
23
24
isString("hello"); // => true
25
isString(new String("hello")); // => false
26
isString(42); // => false
27
```
28
29
### Number Type Checking
30
31
Returns true if value is a number primitive (not Number object).
32
33
```typescript { .api }
34
/**
35
* Returns true if val is number, not Number object
36
* @param val - Value to check
37
* @returns Type guard indicating if value is number
38
*/
39
function isNumber(val?: unknown): val is number;
40
```
41
42
**Usage Example:**
43
44
```typescript
45
import { isNumber } from "is-type-of";
46
47
isNumber(42); // => true
48
isNumber(new Number(42)); // => false
49
isNumber("42"); // => false
50
```
51
52
### Boolean Type Checking
53
54
Returns true if value is a boolean primitive (not Boolean object).
55
56
```typescript { .api }
57
/**
58
* Returns true if val is boolean, not Boolean object
59
* @param val - Value to check
60
* @returns Type guard indicating if value is boolean
61
*/
62
function isBoolean(val?: unknown): val is boolean;
63
```
64
65
### Symbol Type Checking
66
67
Returns true if value is a symbol.
68
69
```typescript { .api }
70
/**
71
* Returns true if val is symbol
72
* @param val - Value to check
73
* @returns Type guard indicating if value is symbol
74
*/
75
function isSymbol(val?: unknown): val is symbol;
76
```
77
78
### Undefined Type Checking
79
80
Returns true if value is undefined.
81
82
```typescript { .api }
83
/**
84
* Returns true if val is undefined
85
* @param val - Value to check
86
* @returns Type guard indicating if value is undefined
87
*/
88
function isUndefined(val?: unknown): val is undefined;
89
```
90
91
### Null Type Checking
92
93
Returns true if value is null.
94
95
```typescript { .api }
96
/**
97
* Returns true if val is null
98
* @param val - Value to check
99
* @returns Type guard indicating if value is null
100
*/
101
function isNull(val?: unknown): val is null;
102
```
103
104
### BigInt Type Checking
105
106
Returns true if value is a bigint.
107
108
```typescript { .api }
109
/**
110
* Returns true if val is bigint
111
* @param val - Value to check
112
* @returns Type guard indicating if value is bigint
113
*/
114
function isBigInt(val?: unknown): val is bigint;
115
```
116
117
### Nullable Type Checking
118
119
Returns true if value is null or undefined.
120
121
```typescript { .api }
122
/**
123
* Returns true if val is null or undefined
124
* @param val - Value to check
125
* @returns Type guard indicating if value is null or undefined
126
*/
127
function isNullable(val?: unknown): val is Nullable;
128
```
129
130
**Usage Example:**
131
132
```typescript
133
import { isNullable } from "is-type-of";
134
135
isNullable(null); // => true
136
isNullable(undefined); // => true
137
isNullable(0); // => false
138
isNullable(""); // => false
139
```
140
141
### Primitive Type Checking
142
143
Returns true if value is any primitive type.
144
145
```typescript { .api }
146
/**
147
* Returns true if val is primitive
148
* @param val - Value to check
149
* @returns Type guard indicating if value is primitive
150
*/
151
function isPrimitive(val?: unknown): val is Primitive;
152
```
153
154
**Usage Example:**
155
156
```typescript
157
import { isPrimitive } from "is-type-of";
158
159
isPrimitive("hello"); // => true
160
isPrimitive(42); // => true
161
isPrimitive(true); // => true
162
isPrimitive({}); // => false
163
isPrimitive([]); // => false
164
```
165
166
## Number Specializations
167
168
### Integer Type Checking
169
170
Returns true if value is an integer.
171
172
```typescript { .api }
173
/**
174
* Returns true if val is integer
175
* @param val - Value to check
176
* @returns Type guard indicating if value is integer
177
*/
178
function isInteger(val?: unknown): val is number;
179
```
180
181
**Usage Example:**
182
183
```typescript
184
import { isInteger } from "is-type-of";
185
186
isInteger(42); // => true
187
isInteger(42.5); // => false
188
isInteger("42"); // => false
189
```
190
191
### 32-bit Integer Type Checking
192
193
Returns true if value is an integer between -2^31 and 2^31-1.
194
195
```typescript { .api }
196
/**
197
* Returns true if val is integer, and between -2 ** 31 and 2 ** 31 - 1
198
* @param val - Value to check
199
* @returns Type guard indicating if value is 32-bit integer
200
*/
201
function isInteger32(val?: unknown): val is number;
202
```
203
204
**Usage Example:**
205
206
```typescript
207
import { isInteger32 } from "is-type-of";
208
209
isInteger32(42); // => true
210
isInteger32(Math.pow(2, 31) - 1); // => true
211
isInteger32(Math.pow(2, 31)); // => false
212
```
213
214
### Long Integer Type Checking
215
216
Returns true if value is an integer outside the 32-bit range.
217
218
```typescript { .api }
219
/**
220
* Returns true if val is integer, and < -2 ** 31, and > 2 ** 31 - 1
221
* @param val - Value to check
222
* @returns Type guard indicating if value is long integer
223
*/
224
function isLong(val?: unknown): val is number;
225
```
226
227
**Usage Example:**
228
229
```typescript
230
import { isLong } from "is-type-of";
231
232
isLong(Math.pow(2, 33)); // => true
233
isLong(42); // => false
234
```
235
236
### Safe Integer Type Checking
237
238
Returns true if value is a safe integer (within JavaScript's safe integer range).
239
240
```typescript { .api }
241
/**
242
* Returns true if val is integer, and between -(2 ** 53 - 1) and 2 ** 53 - 1
243
* @param val - Value to check
244
* @returns Type guard indicating if value is safe integer
245
*/
246
function isSafeInteger(val?: unknown): val is number;
247
```
248
249
### Double Type Checking
250
251
Returns true if value is a floating-point number (has decimal part).
252
253
```typescript { .api }
254
/**
255
* Returns true if val is Double
256
* @param val - Value to check
257
* @returns Type guard indicating if value is double
258
*/
259
function isDouble(val?: unknown): val is number;
260
```
261
262
**Usage Example:**
263
264
```typescript
265
import { isDouble } from "is-type-of";
266
267
isDouble(42.5); // => true
268
isDouble(42); // => false
269
isDouble("42.5"); // => false
270
```
271
272
### NaN Type Checking
273
274
Returns true if value is NaN.
275
276
```typescript { .api }
277
/**
278
* Returns true if val is NaN
279
* @param val - Value to check
280
* @returns Boolean indicating if value is NaN
281
*/
282
function isNaN(val?: unknown): boolean;
283
```
284
285
### Finite Type Checking
286
287
Returns true if value is a finite number.
288
289
```typescript { .api }
290
/**
291
* Returns true if val is finite
292
* @param val - Value to check
293
* @returns Type guard indicating if value is finite number
294
*/
295
function isFinite(val?: unknown): val is number;
296
```
297
298
**Usage Example:**
299
300
```typescript
301
import { isFinite } from "is-type-of";
302
303
isFinite(42); // => true
304
isFinite(Infinity); // => false
305
isFinite(NaN); // => false
306
```
307
308
## Type Definitions
309
310
```typescript { .api }
311
type Primitive = string | number | bigint | boolean | symbol | null | undefined;
312
type Nullable = null | undefined;
313
```