Number
Works with TypeScript number values.
This module exposes the native Number constructor together with helpers for checking, parsing, arithmetic, safe division, comparison, range checks, clamping, rounding, ordering, equivalence, and numeric aggregation.
Constructors
Signature
declare const Number: NumberConstructor;Parses a number from a string safely using the Number() function. The following special string values are supported: "NaN", "Infinity", "-Infinity".
When to use
Use to parse numeric text without throwing on invalid input.
See
Numberfor native constructor coercion
Signature
declare function parse(s: string): Option<number>;Guards
Instances
Equivalence
Equivalence instance for numbers where NaN is considered equal to NaN.
When to use
Use when checking numeric equality through APIs that accept an equivalence relation.
Signature
declare const Equivalence: Equ.Equivalence<number>;Order instance for number values.
When to use
Use when you need to sort or compare numbers through APIs that accept an ordering instance.
Signature
declare const Order: order.Order<number>;Math
Restricts the given number to be within the range specified by the minimum and maximum values.
When to use
Use to force a number into an inclusive range.
Details
- If the number is less than the minimum value, the function returns the minimum value. - If the number is greater than the maximum value, the function returns the maximum value. - Otherwise, it returns the original number.
See
betweenfor checking whether a number is already inside a range
Signature
declare const clamp: {
(options: { maximum: number; minimum: number }): (self: number) => number;
(
self: number,
options: {
maximum: number;
minimum: number;
},
): number;
};Decrements a number by 1.
When to use
Use to decrement a numeric counter by one.
Signature
declare function decrement(n: number): number;Divides numbers safely, returning Option.none() if the divisor is 0.
When to use
Use to divide numbers while representing division by zero as Option.none.
See
divideUnsafefor division that throws when the divisor is zeroremainderfor the numeric remainder operation
Signature
declare const divide: {
(that: number): (self: number) => Option<number>;
(self: number, that: number): Option<number>;
};divideUnsafe
Divides two number values without returning an Option.
When to use
Use to divide number values where the divisor is known to be non-zero and a plain number result is preferred over handling Option.none.
Gotchas
Throws a RangeError if the divisor is 0.
See
dividefor division that returnsOption.nonewhen the divisor is zero
Signature
declare const divideUnsafe: {
(that: number): (self: number) => number;
(self: number, that: number): number;
};Returns the result of adding 1 to a given number.
When to use
Use to increment a numeric counter by one.
Signature
declare function increment(n: number): number;Returns the maximum between two numbers.
When to use
Use to select the larger of two numbers.
See
minfor selecting the smaller value
Signature
declare const max: {
(that: number): (self: number) => number;
(self: number, that: number): number;
};Returns the minimum between two numbers.
When to use
Use to select the smaller of two numbers.
See
maxfor selecting the larger value
Signature
declare const min: {
(that: number): (self: number) => number;
(self: number, that: number): number;
};Provides a multiplication operation on numbers.
When to use
Use to multiply two numbers.
See
multiplyAllfor multiplying an iterable of numbers
Signature
declare const multiply: {
(that: number): (self: number) => number;
(self: number, that: number): number;
};multiplyAll
Takes an Iterable of numbers and returns their multiplication as a single number.
When to use
Use to multiply all numbers in an iterable.
See
multiplyfor multiplying two numbersReducerMultiplyfor multiplying through APIs that consume aReducer
Signature
declare function multiplyAll(collection: Iterable<number>): number;Returns the next power of 2 from the given number.
When to use
Use to round a number up to the next power of two.
Signature
declare function nextPow2(n: number): number;ReducerMax
Reducer for reducing numbers by keeping the maximum value.
When to use
Use to keep the largest number through APIs that consume a Reducer.
Details
The reducer starts from -Infinity, so reducing an empty collection returns -Infinity.
Gotchas
NaN values propagate through Math.max.
See
ReducerMinfor keeping the smallest numbermaxfor comparing two numbers directly
Signature
declare const ReducerMax: Reducer.Reducer<number>;ReducerMin
Reducer for reducing numbers by keeping the minimum value.
When to use
Use to keep the smallest number through APIs that consume a Reducer.
Details
The reducer starts from Infinity, so reducing an empty collection returns Infinity.
Gotchas
NaN values propagate through Math.min.
See
ReducerMaxfor keeping the largest numberminfor comparing two numbers directly
Signature
declare const ReducerMin: Reducer.Reducer<number>;ReducerMultiply
Reducer for combining numbers using multiplication.
When to use
Use to multiply many numbers through APIs that consume a Reducer.
Details
The reducer starts from 1, so reducing an empty collection returns 1.
Gotchas
Reducing an iterable short-circuits when it sees 0, so later elements are not consumed.
See
multiplyAllfor multiplying an iterable directly
Signature
declare const ReducerMultiply: Reducer.Reducer<number>;ReducerSum
Reducer for combining numbers using addition.
When to use
Use to sum many numbers through APIs that consume a Reducer.
Details
The reducer starts from 0, so combineAll([]) returns 0.
See
sumAllfor summing an iterable directlyReducerMultiplyfor multiplying number values
Signature
declare const ReducerSum: Reducer.Reducer<number>;Returns the remainder left over when one operand is divided by a second operand, always taking the sign of the dividend.
When to use
Use to compute a numeric remainder while preserving decimal precision better than direct JavaScript % for decimal operands.
See
dividefor quotient calculation with division-by-zero represented asOption.none
Signature
declare const remainder: {
(divisor: number): (self: number) => number;
(self: number, divisor: number): number;
};Returns the number rounded with the given precision.
When to use
Use to round a number to a fixed number of decimal places.
Signature
declare const round: {
(precision: number): (self: number) => number;
(self: number, precision: number): number;
};Determines the sign of a given number.
When to use
Use to classify a number as negative, zero, or positive.
Signature
declare function sign(n: number): Ordering;Provides a subtraction operation on numbers.
When to use
Use to subtract one number from another.
Signature
declare const subtract: {
(that: number): (self: number) => number;
(self: number, that: number): number;
};Provides an addition operation on numbers.
When to use
Use to add two numbers.
See
sumAllfor summing an iterable of numbers
Signature
declare const sum: {
(that: number): (self: number) => number;
(self: number, that: number): number;
};Takes an Iterable of numbers and returns their sum as a single number.
When to use
Use to sum all numbers in an iterable.
See
sumfor adding two numbersReducerSumfor summing through APIs that consume aReducer
Signature
declare function sumAll(collection: Iterable<number>): number;Predicates
Checks whether a number is between a minimum and maximum value (inclusive).
When to use
Use to test whether a number falls inside an inclusive range.
See
clampfor forcing a number into an inclusive range
Signature
declare const between: {
(options: { maximum: number; minimum: number }): (self: number) => boolean;
(
self: number,
options: {
maximum: number;
minimum: number;
},
): boolean;
};isGreaterThan
Returns true if the first argument is greater than the second, otherwise false.
When to use
Use to test whether one number is strictly greater than another.
Signature
declare const isGreaterThan: {
(that: number): (self: number) => boolean;
(self: number, that: number): boolean;
};isGreaterThanOrEqualTo
Returns a function that checks if a given number is greater than or equal to the provided one.
When to use
Use to test whether one number is greater than or equal to another.
Signature
declare const isGreaterThanOrEqualTo: {
(that: number): (self: number) => boolean;
(self: number, that: number): boolean;
};isLessThan
Returns true if the first argument is less than the second, otherwise false.
When to use
Use to test whether one number is strictly less than another.
Signature
declare const isLessThan: {
(that: number): (self: number) => boolean;
(self: number, that: number): boolean;
};isLessThanOrEqualTo
Returns a function that checks if a given number is less than or equal to the provided one.
When to use
Use to test whether one number is less than or equal to another.
Signature
declare const isLessThanOrEqualTo: {
(that: number): (self: number) => boolean;
(self: number, that: number): boolean;
};
Exposes the global number constructor.
When to use
Use to access native JavaScript numeric coercion from the Effect module namespace.
Gotchas
This follows native
Numbercoercion rules, including empty strings becoming0and invalid numeric strings becomingNaN.See
parsefor parsing strings into anOptionExample (Coercing values to numbers) ``ts import.meta.vitest import { Number as N } from "effect" N.Number("42") // => 42 N.Number("3.14") // => 3.14``