Core Utilities

Number Utilities

Utilities for manipulating and validating numeric types in TypeScript. These types allow you to check parity, sign, get absolute values, truncate decimals, and generate number ranges at the type level.

Number Utilities

Utilities for manipulating and validating numeric types in TypeScript. These types allow you to check parity, sign, get absolute values, truncate decimals, and generate number ranges at the type level.

Installation

npm i -D @halvaradop/ts-utility-types

Usage

import type { , ,  } from "@halvaradop/ts-utility-types/numbers"
import type * as  from "@halvaradop/ts-utility-types/numbers"


type  = .
  • Absolute
  • IsEven
  • IsNegative
  • IsOdd
  • IsPositive
  • NumberRange
  • Trunc

IsOdd<T>

Checks if a number is odd. Returns true if the number is odd, false otherwise.

ArgumentTypeDescription
TnumberThe number to check.

Related: IsEven

import type {  } from "@halvaradop/ts-utility-types/numbers"

type CheckOdd = <2023>
type CheckOdd = true
type CheckEven = <2024>
type CheckEven = false
type CheckOne = <1>
type CheckOne = true

IsEven<T>

Checks if a number is even. Returns true if the number is even, false otherwise.

ArgumentTypeDescription
TnumberThe number to check.

Related: IsOdd

import type {  } from "@halvaradop/ts-utility-types/numbers"

type CheckEven = <2024>
type CheckEven = true
type CheckOdd = <2023>
type CheckOdd = false
type CheckZero = <0>
type CheckZero = true

IsNegative<T>

Checks if a number is negative. Returns true if the number is less than zero, false otherwise.

ArgumentTypeDescription
TnumberThe number to check.

Related: IsPositive

import type {  } from "@halvaradop/ts-utility-types/numbers"

type CheckNegative = <-2024>
type CheckNegative = true
type CheckPositive = <2024>
type CheckPositive = false
type CheckZero = <0>
type CheckZero = false

IsPositive<T>

Checks if a number is positive. Returns true if the number is greater than zero, false otherwise.

ArgumentTypeDescription
TnumberThe number to check.

Related: IsNegative

import type {  } from "@halvaradop/ts-utility-types/numbers"

type CheckPositive = <2024>
type CheckPositive = true
type CheckNegative = <-2024>
type CheckNegative = false
type CheckZero = <0>
type CheckZero = true

Compile-Time Operations

These utilities work with literal numeric types at compile-time. They don't perform runtime operations.

Absolute<Expression>

Returns the absolute value of a number, string, or bigint as a string.

ArgumentTypeDescription
Expressionnumber | string | bigintThe value to get the absolute value of.
import type {  } from "@halvaradop/ts-utility-types/numbers"

type PositiveNumber = <2024>
type PositiveNumber = "2024"
type PositiveString = <"-2024">
type PositiveString = "2024"
type PositiveBigint = <-123n>
type PositiveBigint = "123"

Trunc<Math>

Truncates a number to its integer part, removing any decimal portion.

ArgumentTypeDescription
Mathstring | number | bigintThe number to truncate.
import type {  } from "@halvaradop/ts-utility-types/numbers"

type Truncated = <3.14>
type Truncated = "3"
type TruncatedNegative = <-2.99>
type TruncatedNegative = "-2"
type TruncatedString = <"5.67">
type TruncatedString = "5"

NumberRange<Low, High>

Generates a union type of numeric literals from Low to High (inclusive). Only non-negative values are supported; if either Low or High is negative, the result is never.

ArgumentTypeDescription
LownumberThe starting number of the range (inclusive).
HighnumberThe ending number of the range (inclusive).

Behavior:

import type {  } from "@halvaradop/ts-utility-types/numbers"

type Range1to5 = <1, 5>
type Range1to5 = 1 | 5 | 2 | 3 | 4
type Range3to7 = <3, 7>
type Range3to7 = 5 | 3 | 4 | 7 | 6
type InvalidRange = <-1, 5>
type InvalidRange = never