isPositive
The isPositive utility is a type guard that checks if a given number is strictly greater than zero.
Implementation
View Source Code
ts
/**
* Checks if the value is a positive number.
*
* @example
* ```ts
* isPositive(123); // true
* isPositive(-123); // false
* isPositive(0); // false
* isPositive(0.1); // true
* isPositive(-0.1); // false
* isPositive('hello world'); // false
* isPositive({}); // false
* isPositive([]); // false
* isPositive(new Date()); // false
* isPositive(null); // false
* isPositive(undefined); // false
* isPositive(NaN); // false
* ```
*
* @param arg - The argument to be checked.
*
* @returns `true` if the value is a positive number, else `false`.
*
*/
export function isPositive(arg: unknown): arg is number {
return typeof arg === 'number' && arg > 0;
}Features
- Isomorphic: Works in both Browser and Node.js.
- Strict Check: Only returns
truefor values $> 0$ (zero is not considered positive). - Type-safe: Properly typed for numeric inputs.
API
ts
function isPositive(value: number): boolean;Parameters
value: The number to check.
Returns
trueif the number is greater than zero; otherwise,false.
Examples
Basic Usage
ts
import { isPositive } from '@vielzeug/toolkit';
isPositive(10); // true
isPositive(0.1); // true
isPositive(0); // false
isPositive(-5); // falseImplementation Notes
- Returns
trueifvalue > 0. - Throws nothing; returns
falsefor non-numeric inputs if TypeScript checks are bypassed.
See Also
- isNegative: Check if a number is less than zero.
- isZero: Check if a number is exactly zero.
- isNumber: Check if a value is a number.