guardz
Version:
A simple and lightweight TypeScript type guard library for runtime type validation.
37 lines (36 loc) • 1.32 kB
TypeScript
import type { TypeGuardFn } from './isType';
/**
* Checks if a value is numeric (a number or a string that can be converted to a number).
*
* This type guard validates that a value is either a number or a string that can be
* converted to a number. This is useful for validating numeric values in various contexts
* like form inputs, API data, object keys, etc.
*
* @param value - The value to check
* @param config - Optional configuration for error handling
* @returns True if the value is a number or a string that can be converted to a number, false otherwise
*
* @example
* ```typescript
* import { isNumeric } from 'guardz';
*
* console.log(isNumeric(1)); // true
* console.log(isNumeric("1")); // true
* console.log(isNumeric("123")); // true
* console.log(isNumeric("abc")); // false
* console.log(isNumeric("1.5")); // true
* console.log(isNumeric("1.0")); // true
* console.log(isNumeric("0")); // true
* console.log(isNumeric("")); // false
* console.log(isNumeric(null)); // false
* console.log(isNumeric(undefined)); // false
*
* // With type narrowing
* const data: unknown = getDataFromAPI();
* if (isNumeric(data)) {
* // data is now typed as number
* console.log(Number(data)); // Safe to convert
* }
* ```
*/
export declare const isNumeric: TypeGuardFn<number>;