0
# Number Validation
1
2
Specialized number validation functions providing precise number type checking including NaN handling, positive/negative validation, and integer detection.
3
4
## Capabilities
5
6
### Number Type Checking
7
8
Returns whether the payload is a number (but not NaN). This is the core number validation function that specifically excludes NaN values.
9
10
```typescript { .api }
11
/**
12
* Returns whether the payload is a number (but not NaN)
13
* This will return false for NaN
14
* @param payload - Value to check
15
* @returns Type guard indicating if payload is number (excluding NaN)
16
*/
17
function isNumber(payload: unknown): payload is number;
18
```
19
20
### NaN Value Detection
21
22
Returns whether the payload is literally the value NaN. This is the counterpart to `isNumber()` for detecting NaN values specifically.
23
24
```typescript { .api }
25
/**
26
* Returns whether the payload is literally the value NaN (it's NaN and also a number)
27
* @param payload - Value to check
28
* @returns Type guard indicating if payload is NaN
29
*/
30
function isNaNValue(payload: unknown): payload is typeof Number.NaN;
31
```
32
33
**Usage Examples:**
34
35
```typescript
36
import { isNumber, isNaNValue } from "is-what";
37
38
// Regular numbers
39
isNumber(42); // true
40
isNumber(3.14); // true
41
isNumber(-10); // true
42
43
// NaN handling
44
isNumber(NaN); // false - key difference from typeof
45
isNaNValue(NaN); // true
46
isNaNValue(Number.NaN); // true
47
isNaNValue(0/0); // true
48
49
// Not numbers
50
isNumber("123"); // false
51
isNumber(null); // false
52
isNaNValue("NaN"); // false
53
isNaNValue(undefined); // false
54
55
// Practical usage
56
function safeCalculation(a: unknown, b: unknown) {
57
if (isNumber(a) && isNumber(b)) {
58
return a + b; // TypeScript knows these are numbers
59
}
60
return NaN;
61
}
62
63
function checkForInvalidNumber(value: unknown) {
64
if (isNaNValue(value)) {
65
console.log("Invalid calculation result detected");
66
}
67
}
68
```
69
70
### Integer Validation
71
72
Returns whether the payload is an integer (whole number).
73
74
```typescript { .api }
75
/**
76
* Returns whether the payload is an integer
77
* @param payload - Value to check
78
* @returns Type guard indicating if payload is integer
79
*/
80
function isInteger(payload: unknown): payload is number;
81
```
82
83
**Usage Examples:**
84
85
```typescript
86
import { isInteger } from "is-what";
87
88
isInteger(42); // true
89
isInteger(-10); // true
90
isInteger(0); // true
91
isInteger(3.14); // false
92
isInteger(3.0); // true (JavaScript treats 3.0 as integer)
93
isInteger(NaN); // false
94
isInteger("42"); // false
95
96
if (isInteger(value)) {
97
// Safe to use as array index or loop counter
98
console.log(`Processing item ${value}`);
99
}
100
```
101
102
### Positive Number Validation
103
104
Returns whether the payload is a positive number (greater than zero).
105
106
```typescript { .api }
107
/**
108
* Returns whether the payload is a positive number
109
* @param payload - Value to check
110
* @returns Type guard indicating if payload is positive number
111
*/
112
function isPositiveNumber(payload: unknown): payload is number;
113
```
114
115
**Usage Examples:**
116
117
```typescript
118
import { isPositiveNumber } from "is-what";
119
120
isPositiveNumber(42); // true
121
isPositiveNumber(0.1); // true
122
isPositiveNumber(0); // false
123
isPositiveNumber(-5); // false
124
isPositiveNumber(NaN); // false
125
isPositiveNumber("10"); // false
126
127
// Useful for validation
128
function calculateArea(width: unknown, height: unknown) {
129
if (isPositiveNumber(width) && isPositiveNumber(height)) {
130
return width * height;
131
}
132
throw new Error("Width and height must be positive numbers");
133
}
134
```
135
136
### Negative Number Validation
137
138
Returns whether the payload is a negative number (less than zero).
139
140
```typescript { .api }
141
/**
142
* Returns whether the payload is a negative number
143
* @param payload - Value to check
144
* @returns Type guard indicating if payload is negative number
145
*/
146
function isNegativeNumber(payload: unknown): payload is number;
147
```
148
149
**Usage Examples:**
150
151
```typescript
152
import { isNegativeNumber } from "is-what";
153
154
isNegativeNumber(-42); // true
155
isNegativeNumber(-0.1); // true
156
isNegativeNumber(0); // false
157
isNegativeNumber(5); // false
158
isNegativeNumber(NaN); // false
159
isNegativeNumber("-10"); // false
160
161
// Practical usage
162
function validateTemperature(temp: unknown) {
163
if (isNegativeNumber(temp)) {
164
console.log("Below freezing temperature detected");
165
return temp; // TypeScript knows this is a number
166
}
167
}
168
```
169
170
## Combined Usage Patterns
171
172
```typescript
173
import {
174
isNumber,
175
isNaNValue,
176
isInteger,
177
isPositiveNumber,
178
isNegativeNumber
179
} from "is-what";
180
181
function analyzeNumber(value: unknown) {
182
if (isNaNValue(value)) {
183
return "Invalid number (NaN)";
184
}
185
186
if (!isNumber(value)) {
187
return "Not a number";
188
}
189
190
const characteristics = [];
191
192
if (isInteger(value)) {
193
characteristics.push("integer");
194
} else {
195
characteristics.push("decimal");
196
}
197
198
if (isPositiveNumber(value)) {
199
characteristics.push("positive");
200
} else if (isNegativeNumber(value)) {
201
characteristics.push("negative");
202
} else {
203
characteristics.push("zero");
204
}
205
206
return `Number: ${value} (${characteristics.join(", ")})`;
207
}
208
209
// Examples
210
analyzeNumber(42); // "Number: 42 (integer, positive)"
211
analyzeNumber(-3.14); // "Number: -3.14 (decimal, negative)"
212
analyzeNumber(0); // "Number: 0 (integer, zero)"
213
analyzeNumber(NaN); // "Invalid number (NaN)"
214
analyzeNumber("123"); // "Not a number"
215
```