-
Notifications
You must be signed in to change notification settings - Fork 0
/
aux.ts
56 lines (53 loc) · 1.71 KB
/
aux.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
import {
Constants,
ConstantsWrapper,
InferTaggedConstants,
InferUntaggedConstants,
UntagTaggedConstants,
} from "../types/consts.js";
import { TagSupertype } from "../types/tag.js";
import { constants } from "./constants.js";
/**
* Create a Constants object from the keys of a tagged or untagged record of constants
*
* @param tag - The tag to set to enable nominal typing
* @param mcs - The input record
* @returns A Constants object where both the keys and their values are the ones from the input record
*/
export function deriveConstants<
Tag extends TagSupertype,
MCs extends
| InferTaggedConstants<Constants>
| InferUntaggedConstants<Constants>,
>(tag: Tag, mcs: MCs): ConstantsWrapper<{ [K in keyof MCs]: K }, Tag> {
return constants(tag, Object.keys(mcs)) as ConstantsWrapper<
{ [K in keyof MCs]: K },
Tag
>;
}
/**
* Create a record of untagged constants from the keys of a tagged or untagged record of constants
*
* @param mcs - The input record
* @returns A Constants object where both the keys and their values are the ones from the input record
*/
export function deriveUntaggedConstants<
MCs extends
| InferTaggedConstants<Constants>
| InferUntaggedConstants<Constants>,
>(
mcs: MCs,
): InferUntaggedConstants<ConstantsWrapper<{ [K in keyof MCs]: K }, never>> {
return deriveConstants(void 0 as unknown as TagSupertype, mcs).untagged;
}
/**
* Remove the tag from the types of the constants contained into the input record of tagged constants
*
* @param tcs - The input record
* @returns an untagged shallow clone of tcs
*/
export function removeTags<TCs extends InferTaggedConstants<Constants>>(
tcs: TCs,
): UntagTaggedConstants<TCs> {
return { ...tcs } as UntagTaggedConstants<TCs>;
}