-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperators.ts
44 lines (28 loc) · 975 Bytes
/
operators.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
import { Units } from './base';
import { Add, Numbers, Negate, MulN } from './math';
export type Pow<U extends Units, N extends Numbers> =
{ [P in keyof U]: MulN<U[P], N> }
export type Inverse<U extends string | Units> =
U extends string ? { [P in U]: -1 }
: U extends Units ? InvertO<U>
: {};
export type Mul<A extends Units, B extends Units> =
{ [P in MulKeys<A, B>]:
P extends keyof B ?
(P extends keyof A ? Add<A[P], B[P]> : B[P])
: P extends keyof A ? A[P]
: 'out-of-bounds'
}
export type Div<A extends Units, B extends Units> =
Mul<A, InvertO<B>>;
export type InvertO<O extends object> =
{ [P in keyof O]: Negate<O[P]> }
type MulKeys<A extends object, B extends object> =
Exclude<keyof A | keyof B, ReducedKeys<A, B>>;
type ReducedKeys<A extends object, B extends object> = {
[P in keyof B]: (
P extends keyof A ? (
Add<A[P], B[P]> extends ReducedV ? P : never
) : never)
}[keyof B];
type ReducedV = 0;