UNPKG

1.47 kBTypeScriptView Raw
1import type {NumberAbsolute, PositiveNumericStringGt} from './internal';
2import type {IsEqual} from './is-equal';
3import type {PositiveInfinity, NegativeInfinity, IsNegative} from './numeric';
4import type {And} from './and';
5import type {Or} from './or';
6
7/**
8Returns a boolean for whether a given number is greater than another number.
9
10@example
11```
12import type {GreaterThan} from 'type-fest';
13
14GreaterThan<1, -5>;
15//=> true
16
17GreaterThan<1, 1>;
18//=> false
19
20GreaterThan<1, 5>;
21//=> false
22```
23*/
24export type GreaterThan<A extends number, B extends number> = number extends A | B
25 ? never
26 : [
27 IsEqual<A, PositiveInfinity>, IsEqual<A, NegativeInfinity>,
28 IsEqual<B, PositiveInfinity>, IsEqual<B, NegativeInfinity>,
29 ] extends infer R extends [boolean, boolean, boolean, boolean]
30 ? Or<
31 And<IsEqual<R[0], true>, IsEqual<R[2], false>>,
32 And<IsEqual<R[3], true>, IsEqual<R[1], false>>
33 > extends true
34 ? true
35 : Or<
36 And<IsEqual<R[1], true>, IsEqual<R[3], false>>,
37 And<IsEqual<R[2], true>, IsEqual<R[0], false>>
38 > extends true
39 ? false
40 : true extends R[number]
41 ? false
42 : [IsNegative<A>, IsNegative<B>] extends infer R extends [boolean, boolean]
43 ? [true, false] extends R
44 ? false
45 : [false, true] extends R
46 ? true
47 : [false, false] extends R
48 ? PositiveNumericStringGt<`${A}`, `${B}`>
49 : PositiveNumericStringGt<`${NumberAbsolute<B>}`, `${NumberAbsolute<A>}`>
50 : never
51 : never;