UNPKG

type-fest

Version:

A collection of essential TypeScript types

67 lines (46 loc) • 2.13 kB
import type {GreaterThanOrEqual} from './greater-than-or-equal.d.ts'; import type {_Numeric} from './numeric.d.ts'; /** Returns a boolean for whether a given number is less than or equal to another number. Distributes over both operands; mixed results return `boolean`. A `never` operand returns `false`. Otherwise, an `any` operand returns `boolean`. Only numeric operands are accepted. @example ``` import type {LessThanOrEqual} from 'type-fest'; type A = LessThanOrEqual<1, -5>; //=> false type B = LessThanOrEqual<1, 1>; //=> true type C = LessThanOrEqual<1, 5>; //=> true type D = LessThanOrEqual<5n, 5>; //=> true ``` Note: If either argument is the non-literal `number` or `bigint` type, the result is `boolean`. Note: Two numbers in exponential notation with exponents of the same sign return `boolean` unless equality or the numbers' signs decide the comparison. Comparing a `bigint` with a `number` of `1e+21` or above (or `-1e+21` or below) also returns `boolean`. Other comparisons with a number in exponential notation are still decided. Note: A `number` above `Number.MAX_SAFE_INTEGER` is compared with a `bigint` by its printed digits, not its exact value. For example, `123456789012345680000` is really `123456789012345683968`, so comparing it with `123456789012345680000n` treats them as equal. @example ``` import type {LessThanOrEqual} from 'type-fest'; type A = LessThanOrEqual<number, 1>; //=> boolean type B = LessThanOrEqual<1, number>; //=> boolean type C = LessThanOrEqual<number, number>; //=> boolean ``` @example ``` import type {LessThanOrEqual} from 'type-fest'; // Use `LessThanOrEqual` to constrain a function parameter to non-positive numbers. declare function setNonPositive<N extends number>(value: LessThanOrEqual<N, 0> extends true ? N : never): void; setNonPositive(0); // ✅ Allowed setNonPositive(-1); // ✅ Allowed // @ts-expect-error setNonPositive(1); // @ts-expect-error setNonPositive(2); ``` */ // Reuse the inclusive comparison with its operands reversed. export type LessThanOrEqual<A extends _Numeric, B extends _Numeric> = GreaterThanOrEqual<B, A>; export {};