type-fest
Version:
A collection of essential TypeScript types
118 lines (94 loc) • 4.41 kB
TypeScript
import type {PositiveNumericStringGt} from './internal/index.d.ts';
import type {PositiveInfinity, NegativeInfinity, IsNegative, _Numeric} from './numeric.d.ts';
import type {Absolute} from './absolute.d.ts';
import type {UnwrapBrandedPrimitive} from './unwrap-branded-primitive.d.ts';
import type {IsAny} from './is-any.d.ts';
/**
Returns a boolean for whether a given number is greater than 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 {GreaterThan} from 'type-fest';
type A = GreaterThan<1, -5>;
//=> true
type B = GreaterThan<1, 1>;
//=> false
type C = GreaterThan<1, 5>;
//=> false
type D = GreaterThan<5n, 1>;
//=> 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 {GreaterThan} from 'type-fest';
type A = GreaterThan<number, 1>;
//=> boolean
type B = GreaterThan<1, number>;
//=> boolean
type C = GreaterThan<number, number>;
//=> boolean
type D = GreaterThan<bigint, 1n>;
//=> boolean
```
@example
```
import type {GreaterThan} from 'type-fest';
// Use `GreaterThan` to constrain a function parameter to positive numbers.
declare function setPositive<N extends number>(value: GreaterThan<N, 0> extends true ? N : never): void;
setPositive(1); // ✅ Allowed
setPositive(2); // ✅ Allowed
// @ts-expect-error
setPositive(0);
// @ts-expect-error
setPositive(-1);
```
*/
export type GreaterThan<A extends _Numeric, B extends _Numeric> =
// `any` is checked before `never`, so generic code that wraps the result keeps resolving it after instantiation. A `never` operand would otherwise make the distribution below return `never`.
IsAny<A | B> extends true
? [A, B] extends [never, unknown] | [unknown, never] ? false : boolean
: [A, B] extends [never, unknown] | [unknown, never] ? false
: _GreaterThan<A extends object ? UnwrapBrandedPrimitive<A> : A, B extends object ? UnwrapBrandedPrimitive<B> : B>;
// Numeric enum members accept `number`, but their printed types still identify their known values.
type _GreaterThan<A extends _Numeric, B extends _Numeric> =
A extends _Numeric // For distributing `A`
? B extends _Numeric // For distributing `B`
? `${number}` extends `${A | B}`
? boolean
: bigint extends A | B
? boolean
// A `number` of `1e+21` and above is printed in exponential notation, which cannot be compared digit by digit with the plain digits of a `bigint`.
: IsBigIntAgainstLargeExponent<A, B> extends true
? boolean
: A extends PositiveInfinity
? B extends PositiveInfinity ? false : true
: B extends NegativeInfinity
? A extends NegativeInfinity ? false : true
: A extends NegativeInfinity
? false
: B extends PositiveInfinity
? false
: [IsNegative<A>, IsNegative<B>] extends infer Signs extends [boolean, boolean]
? [true, false] extends Signs
? false
: [false, true] extends Signs
? true
: [false, false] extends Signs
? PositiveNumericStringGt<`${A}`, `${B}`>
: PositiveNumericStringGt<`${Absolute<B>}`, `${Absolute<A>}`>
: never
: never // Should never happen
: never; // Should never happen
/**
Whether one of the values is a `bigint` and the other is a `number` of `1e+21` and above, which is printed in exponential notation.
*/
type IsBigIntAgainstLargeExponent<A extends _Numeric, B extends _Numeric> =
A extends bigint
? `${B}` extends `${string}e+${string}` ? true : false
: B extends bigint
? `${A}` extends `${string}e+${string}` ? true : false
: false;
export {};