A utility function to get the type of a value
npx @tessl/cli install tessl/npm-jest-get-type@29.6.00
# jest-get-type
1
2
A utility function to get the type of a value with handling for edge cases like arrays and null values that the native `typeof` operator doesn't address correctly.
3
4
## Package Information
5
6
- **Package Name**: jest-get-type
7
- **Package Type**: npm
8
- **Language**: TypeScript
9
- **Installation**: `npm install jest-get-type`
10
11
## Core Imports
12
13
```typescript
14
import { getType, isPrimitive } from "jest-get-type";
15
```
16
17
For CommonJS:
18
19
```javascript
20
const { getType, isPrimitive } = require("jest-get-type");
21
```
22
23
## Basic Usage
24
25
```typescript
26
import { getType, isPrimitive } from "jest-get-type";
27
28
// Get accurate type information
29
console.log(getType([])); // "array" (not "object")
30
console.log(getType(null)); // "null" (not "object")
31
console.log(getType(new Date())); // "date"
32
console.log(getType(/regex/)); // "regexp"
33
console.log(getType(new Map())); // "map"
34
35
// Check if value is primitive
36
console.log(isPrimitive("hello")); // true
37
console.log(isPrimitive(42)); // true
38
console.log(isPrimitive(null)); // true
39
console.log(isPrimitive([])); // false
40
console.log(isPrimitive({})); // false
41
```
42
43
## Capabilities
44
45
### Type Detection
46
47
Get the precise type of a value, handling edge cases that native `typeof` doesn't address correctly.
48
49
```typescript { .api }
50
/**
51
* Get the type of a value with handling for edge cases like arrays and null
52
* @param value - Any value to determine the type of
53
* @returns Precise type string
54
* @throws Error for values of unknown type
55
*/
56
function getType(value: unknown): ValueType;
57
```
58
59
The `getType` function provides more accurate type detection than native `typeof` by:
60
61
- Correctly identifying arrays as `"array"` instead of `"object"`
62
- Properly handling `null` as `"null"` instead of `"object"`
63
- Detecting specific object subtypes: `RegExp`, `Map`, `Set`, `Date`
64
- Comprehensive coverage of all JavaScript primitive and built-in object types
65
66
**Usage Examples:**
67
68
```typescript
69
// Primitive types
70
getType(undefined); // "undefined"
71
getType(null); // "null"
72
getType(true); // "boolean"
73
getType(42); // "number"
74
getType("hello"); // "string"
75
getType(Symbol("id")); // "symbol"
76
getType(BigInt(123)); // "bigint"
77
78
// Function type
79
getType(() => {}); // "function"
80
81
// Object subtypes
82
getType([1, 2, 3]); // "array"
83
getType({}); // "object"
84
getType(/pattern/); // "regexp"
85
getType(new Map()); // "map"
86
getType(new Set()); // "set"
87
getType(new Date()); // "date"
88
```
89
90
### Primitive Type Checking
91
92
Determine if a value is a primitive type using the standard JavaScript definition.
93
94
```typescript { .api }
95
/**
96
* Determine if a value is a primitive type
97
* @param value - Any value to check
98
* @returns true if the value is primitive, false otherwise
99
*/
100
function isPrimitive(value: unknown): boolean;
101
```
102
103
Uses the standard test `Object(value) !== value` to determine primitiveness. Primitive types in JavaScript are:
104
- `null`
105
- `undefined`
106
- `boolean`
107
- `number` (including `NaN` and `Infinity`)
108
- `string`
109
- `symbol`
110
- `bigint`
111
112
**Usage Examples:**
113
114
```typescript
115
// Primitive values
116
isPrimitive(null); // true
117
isPrimitive(undefined); // true
118
isPrimitive(42); // true
119
isPrimitive("text"); // true
120
isPrimitive(true); // true
121
isPrimitive(Symbol("x")); // true
122
isPrimitive(BigInt(999)); // true
123
isPrimitive(NaN); // true
124
isPrimitive(Infinity); // true
125
126
// Non-primitive values
127
isPrimitive({}); // false
128
isPrimitive([]); // false
129
isPrimitive(() => {}); // false
130
isPrimitive(/regex/); // false
131
isPrimitive(new Map()); // false
132
isPrimitive(new Set()); // false
133
isPrimitive(new Date()); // false
134
```
135
136
## Return Types
137
138
The `getType` function returns one of the following string literals:
139
140
```typescript
141
type ValueType =
142
| 'array'
143
| 'bigint'
144
| 'boolean'
145
| 'function'
146
| 'null'
147
| 'number'
148
| 'object'
149
| 'regexp'
150
| 'map'
151
| 'set'
152
| 'date'
153
| 'string'
154
| 'symbol'
155
| 'undefined';
156
```
157
158
*Note: `ValueType` is an internal type definition used for documentation purposes. It is not exported from the package and cannot be imported directly. The type information is provided here for reference to show all possible return values from the `getType` function.*
159
160
## Error Handling
161
162
The `getType` function throws an `Error` with the message `"value of unknown type: ${value}"` if it encounters a value that doesn't match any of the expected type patterns. This is a fallback case that should not occur under normal circumstances with standard JavaScript values.