-
Notifications
You must be signed in to change notification settings - Fork 23
/
index.ts
39 lines (32 loc) · 988 Bytes
/
index.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
type Length<T extends any[]> = T["length"] & number;
type Push<T extends any[], Val> = [...T, Val];
type NTuple<N extends number, T extends any[] = []> = T["length"] extends N
? T
: NTuple<N, Push<T, any>>;
export type Add<A extends number, B extends number> = Length<
[...NTuple<A>, ...NTuple<B>]
>;
export type Subtract<A extends number, B extends number> = NTuple<A> extends [
...infer U,
...NTuple<B>
]
? Length<U>
: never;
type MultiAdd<
N extends number,
A extends number,
I extends number
> = I extends 0 ? A : MultiAdd<N, Add<N, A>, Subtract<I, 1>>;
type Multiply<A extends number, B extends number> = MultiAdd<A, 0, B>;
type Comparator<N1 extends number, N2 extends number> = N1 extends N2
? false
: [Subtract<N2, N1>] extends [never]
? true
: false;
type Demo1 = Add<2, 3>;
type Demo2 = Subtract<2, 3>;
type Demo3 = Multiply<2, 3>;
type Demo4 = Comparator<2, 3>;
type Demo5 = Comparator<5, 3>;
// type Test = Add<1099123, 9901123>;
export {};