-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathone_of.ts
32 lines (24 loc) · 951 Bytes
/
one_of.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
type BaseMessage = { id: string, timestamp: number };
type TextMessage = BaseMessage & { text: string; };
type ImgMessage = BaseMessage & { imgPath: string };
type UrlMessage = BaseMessage & { url: string; };
type Message = TextMessage | UrlMessage | ImgMessage;
type MessageTypesArray = OneOf<[TextMessage, UrlMessage, ImgMessage]>;
type MergeTypes<TypesArray extends any[], Res = {}> =
TypesArray extends [infer Head, ...infer Rem]
? MergeTypes<Rem, Res & Head>
: Res;
type OneOf<
TypesArray extends any[],
Res = never,
AllProperties = MergeTypes<TypesArray>> =
TypesArray extends [infer Head, ...infer Rem]
? OneOf<Rem, Res | OnlyFirst<Head, AllProperties>, AllProperties>
: Res;
type SimpleOneOf<F, S> = OnlyFirst<F, S> | OnlyFirst<S, F>;
type OnlyFirst<F, S> = F & {[Key in keyof Omit<S, keyof F>]?: never};
const message: MessageTypesArray = {
id: '1',
timestamp: new Date().getTime(),
imgPath: 'path'
}