-
-
Notifications
You must be signed in to change notification settings - Fork 547
/
sum.d.ts
70 lines (61 loc) · 1.91 KB
/
sum.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import type {NumberAbsolute, BuildTuple, TupleMax, TupleMin} from './internal';
import type {IsEqual} from './is-equal';
import type {PositiveInfinity, NegativeInfinity, IsNegative} from './numeric';
import type {Subtract} from './subtract';
import type {And} from './and';
import type {Or} from './or';
/**
Returns the sum of two numbers.
Note:
- A or B can only support `-999` ~ `999`.
- A and B can only be small integers, less than 1000.
- If the result is negative, you can only get `number`.
@example
```
import type {Sum} from 'type-fest';
Sum<111, 222>;
//=> 333
Sum<-111, 222>;
//=> 111
Sum<111, -222>;
//=> number
Sum<PositiveInfinity, -9999>;
//=> PositiveInfinity
Sum<PositiveInfinity, NegativeInfinity>;
//=> number
```
@category Numeric
*/
// TODO: Support big integer and negative number.
export type Sum<A extends number, B extends number> = number extends A | B
? number
: [
IsEqual<A, PositiveInfinity>, IsEqual<A, NegativeInfinity>,
IsEqual<B, PositiveInfinity>, IsEqual<B, NegativeInfinity>,
] extends infer R extends [boolean, boolean, boolean, boolean]
? Or<
And<IsEqual<R[0], true>, IsEqual<R[3], false>>,
And<IsEqual<R[2], true>, IsEqual<R[1], false>>
> extends true
? PositiveInfinity
: Or<
And<IsEqual<R[1], true>, IsEqual<R[2], false>>,
And<IsEqual<R[3], true>, IsEqual<R[0], false>>
> extends true
? NegativeInfinity
: true extends R[number]
? number
: ([IsNegative<A>, IsNegative<B>] extends infer R
? [false, false] extends R
? [...BuildTuple<A>, ...BuildTuple<B>]['length']
: [true, true] extends R
? number
: TupleMax<[NumberAbsolute<A>, NumberAbsolute<B>]> extends infer Max_
? TupleMin<[NumberAbsolute<A>, NumberAbsolute<B>]> extends infer Min_ extends number
? Max_ extends A | B
? Subtract<Max_, Min_>
: number
: never
: never
: never) & number
: never;