-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag.ts
38 lines (34 loc) · 913 Bytes
/
tag.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
/**
* Private unique symbol to enable nominal typing
* @internal
*/
declare const _tag: unique symbol;
/**
* The supertype of tags
*/
export type TagSupertype = PropertyKey;
/**
* Tags a type using the private unique symbol and a custom defined tag to enable nominal typing
* @internal
*
* @typeParam Tag - The custom defined tag to set
* @returns The tagged type
*/
type _Tagged<Tag extends TagSupertype> = {
readonly [_tag]: Tag;
};
/**
* Tags a type using a custom defined tag to enable nominal typing
*
* @typeParam Tag - The custom defined tag to set
* @returns The tagged type
*/
export type Tagged<T, Tag extends TagSupertype> = T & _Tagged<Tag>;
/**
* Removes the tag from a tagged type
*
* @typeParam T - The tagged type
* @returns The untagged type
*/
export type RemoveTag<T extends Tagged<unknown, TagSupertype>> =
T extends Tagged<infer Type, TagSupertype> ? Type : never;