JavaScript and TypeScript type checking utility functions providing precise type validation with automatic type narrowing.
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Specialized number validation functions providing precise number type checking including NaN handling, positive/negative validation, and integer detection.
Returns whether the payload is a number (but not NaN). This is the core number validation function that specifically excludes NaN values.
/**
* Returns whether the payload is a number (but not NaN)
* This will return false for NaN
* @param payload - Value to check
* @returns Type guard indicating if payload is number (excluding NaN)
*/
function isNumber(payload: unknown): payload is number;Returns whether the payload is literally the value NaN. This is the counterpart to isNumber() for detecting NaN values specifically.
/**
* Returns whether the payload is literally the value NaN (it's NaN and also a number)
* @param payload - Value to check
* @returns Type guard indicating if payload is NaN
*/
function isNaNValue(payload: unknown): payload is typeof Number.NaN;Usage Examples:
import { isNumber, isNaNValue } from "is-what";
// Regular numbers
isNumber(42); // true
isNumber(3.14); // true
isNumber(-10); // true
// NaN handling
isNumber(NaN); // false - key difference from typeof
isNaNValue(NaN); // true
isNaNValue(Number.NaN); // true
isNaNValue(0/0); // true
// Not numbers
isNumber("123"); // false
isNumber(null); // false
isNaNValue("NaN"); // false
isNaNValue(undefined); // false
// Practical usage
function safeCalculation(a: unknown, b: unknown) {
if (isNumber(a) && isNumber(b)) {
return a + b; // TypeScript knows these are numbers
}
return NaN;
}
function checkForInvalidNumber(value: unknown) {
if (isNaNValue(value)) {
console.log("Invalid calculation result detected");
}
}Returns whether the payload is an integer (whole number).
/**
* Returns whether the payload is an integer
* @param payload - Value to check
* @returns Type guard indicating if payload is integer
*/
function isInteger(payload: unknown): payload is number;Usage Examples:
import { isInteger } from "is-what";
isInteger(42); // true
isInteger(-10); // true
isInteger(0); // true
isInteger(3.14); // false
isInteger(3.0); // true (JavaScript treats 3.0 as integer)
isInteger(NaN); // false
isInteger("42"); // false
if (isInteger(value)) {
// Safe to use as array index or loop counter
console.log(`Processing item ${value}`);
}Returns whether the payload is a positive number (greater than zero).
/**
* Returns whether the payload is a positive number
* @param payload - Value to check
* @returns Type guard indicating if payload is positive number
*/
function isPositiveNumber(payload: unknown): payload is number;Usage Examples:
import { isPositiveNumber } from "is-what";
isPositiveNumber(42); // true
isPositiveNumber(0.1); // true
isPositiveNumber(0); // false
isPositiveNumber(-5); // false
isPositiveNumber(NaN); // false
isPositiveNumber("10"); // false
// Useful for validation
function calculateArea(width: unknown, height: unknown) {
if (isPositiveNumber(width) && isPositiveNumber(height)) {
return width * height;
}
throw new Error("Width and height must be positive numbers");
}Returns whether the payload is a negative number (less than zero).
/**
* Returns whether the payload is a negative number
* @param payload - Value to check
* @returns Type guard indicating if payload is negative number
*/
function isNegativeNumber(payload: unknown): payload is number;Usage Examples:
import { isNegativeNumber } from "is-what";
isNegativeNumber(-42); // true
isNegativeNumber(-0.1); // true
isNegativeNumber(0); // false
isNegativeNumber(5); // false
isNegativeNumber(NaN); // false
isNegativeNumber("-10"); // false
// Practical usage
function validateTemperature(temp: unknown) {
if (isNegativeNumber(temp)) {
console.log("Below freezing temperature detected");
return temp; // TypeScript knows this is a number
}
}import {
isNumber,
isNaNValue,
isInteger,
isPositiveNumber,
isNegativeNumber
} from "is-what";
function analyzeNumber(value: unknown) {
if (isNaNValue(value)) {
return "Invalid number (NaN)";
}
if (!isNumber(value)) {
return "Not a number";
}
const characteristics = [];
if (isInteger(value)) {
characteristics.push("integer");
} else {
characteristics.push("decimal");
}
if (isPositiveNumber(value)) {
characteristics.push("positive");
} else if (isNegativeNumber(value)) {
characteristics.push("negative");
} else {
characteristics.push("zero");
}
return `Number: ${value} (${characteristics.join(", ")})`;
}
// Examples
analyzeNumber(42); // "Number: 42 (integer, positive)"
analyzeNumber(-3.14); // "Number: -3.14 (decimal, negative)"
analyzeNumber(0); // "Number: 0 (integer, zero)"
analyzeNumber(NaN); // "Invalid number (NaN)"
analyzeNumber("123"); // "Not a number"