0
# Type Checking Utilities
1
2
Comprehensive type checking functions for runtime type validation and type guards. These utilities help with safe type checking in dynamic JavaScript environments.
3
4
## Capabilities
5
6
### Array Type Checking
7
8
Check if a value is an array.
9
10
```typescript { .api }
11
/**
12
* Checks if a value is an array
13
* @param val - The value to check
14
* @returns Type guard indicating if value is an array
15
*/
16
const isArray: (val: unknown) => val is any[];
17
```
18
19
**Usage Example:**
20
21
```typescript
22
import { isArray } from "@intlify/shared";
23
24
const value = [1, 2, 3];
25
if (isArray(value)) {
26
// TypeScript now knows value is an array
27
console.log(value.length); // Safe to use array methods
28
}
29
```
30
31
### String Type Checking
32
33
Check if a value is a string.
34
35
```typescript { .api }
36
/**
37
* Checks if a value is a string
38
* @param val - The value to check
39
* @returns Type guard indicating if value is a string
40
*/
41
function isString(val: unknown): val is string;
42
```
43
44
### Object Type Checking
45
46
Check if a value is an object (non-null and not an array).
47
48
```typescript { .api }
49
/**
50
* Checks if a value is an object (non-null, non-array)
51
* @param val - The value to check
52
* @returns Type guard indicating if value is an object
53
*/
54
function isObject(val: unknown): val is Record<any, any>;
55
```
56
57
### Function Type Checking
58
59
Check if a value is a function.
60
61
```typescript { .api }
62
/**
63
* Checks if a value is a function
64
* @param val - The value to check
65
* @returns Type guard indicating if value is a function
66
*/
67
function isFunction(val: unknown): val is Function;
68
```
69
70
### Boolean Type Checking
71
72
Check if a value is a boolean.
73
74
```typescript { .api }
75
/**
76
* Checks if a value is a boolean
77
* @param val - The value to check
78
* @returns Type guard indicating if value is a boolean
79
*/
80
function isBoolean(val: unknown): val is boolean;
81
```
82
83
### Symbol Type Checking
84
85
Check if a value is a symbol.
86
87
```typescript { .api }
88
/**
89
* Checks if a value is a symbol
90
* @param val - The value to check
91
* @returns Type guard indicating if value is a symbol
92
*/
93
function isSymbol(val: unknown): val is symbol;
94
```
95
96
### Number Type Checking
97
98
Check if a value is a finite number.
99
100
```typescript { .api }
101
/**
102
* Checks if a value is a finite number
103
* @param val - The value to check
104
* @returns Type guard indicating if value is a finite number
105
*/
106
function isNumber(val: unknown): val is number;
107
```
108
109
### Date Type Checking
110
111
Check if a value is a Date object.
112
113
```typescript { .api }
114
/**
115
* Checks if a value is a Date object
116
* @param val - The value to check
117
* @returns Type guard indicating if value is a Date
118
*/
119
function isDate(val: unknown): val is Date;
120
```
121
122
### RegExp Type Checking
123
124
Check if a value is a RegExp object.
125
126
```typescript { .api }
127
/**
128
* Checks if a value is a RegExp object
129
* @param val - The value to check
130
* @returns Type guard indicating if value is a RegExp
131
*/
132
function isRegExp(val: unknown): val is RegExp;
133
```
134
135
### Promise Type Checking
136
137
Check if a value is a Promise-like object (has then and catch methods).
138
139
```typescript { .api }
140
/**
141
* Checks if a value is a Promise-like object
142
* @param val - The value to check
143
* @returns Type guard indicating if value is a Promise
144
*/
145
function isPromise<T = any>(val: unknown): val is Promise<T>;
146
```
147
148
### Plain Object Type Checking
149
150
Check if a value is a plain object (created by Object constructor or object literal).
151
152
```typescript { .api }
153
/**
154
* Checks if a value is a plain object
155
* @param val - The value to check
156
* @returns Type guard indicating if value is a plain object
157
*/
158
function isPlainObject(val: unknown): val is object;
159
```
160
161
### Empty Object Type Checking
162
163
Check if a value is an empty plain object.
164
165
```typescript { .api }
166
/**
167
* Checks if a value is an empty plain object
168
* Note: Return type annotation in source code may be incorrect
169
* @param val - The value to check
170
* @returns Type guard (source declares 'val is boolean' but likely should be 'val is object')
171
*/
172
function isEmptyObject(val: unknown): val is boolean;
173
```
174
175
## Type Utilities
176
177
### Type String Conversion
178
179
Get the type string representation of a value.
180
181
```typescript { .api }
182
/**
183
* Gets the type string representation of a value
184
* @param value - The value to get type string for
185
* @returns Type string like '[object Object]', '[object Array]', etc.
186
*/
187
function toTypeString(value: unknown): string;
188
189
/**
190
* Reference to Object.prototype.toString
191
*/
192
const objectToString: () => string;
193
```
194
195
**Usage Example:**
196
197
```typescript
198
import { isObject, isArray, isString, isNumber } from "@intlify/shared";
199
200
function processValue(value: unknown) {
201
if (isString(value)) {
202
return value.toUpperCase();
203
} else if (isNumber(value)) {
204
return value * 2;
205
} else if (isArray(value)) {
206
return value.map(processValue);
207
} else if (isObject(value)) {
208
// Safe to iterate over object properties
209
return Object.keys(value).reduce((acc, key) => {
210
acc[key] = processValue(value[key]);
211
return acc;
212
}, {} as any);
213
}
214
return value;
215
}
216
```