-
Notifications
You must be signed in to change notification settings - Fork 0
/
dto.ts
43 lines (37 loc) · 1.28 KB
/
dto.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
/* eslint-disable @typescript-eslint/ban-types */
/* eslint-disable @typescript-eslint/no-explicit-any */
type IsOptional<T> = Extract<T, undefined> extends never ? false : true
export type Func = (...args: any[]) => any
type IsFunction<T> = T extends Func ? true : false
type IsValueType<T> = T extends
| string
| number
| boolean
| null
| undefined
| Func
| Set<any>
| Map<any, any>
| Date
| Array<any>
? true
: false
type ReplaceDate<T> = T extends Date ? string : T
type ReplaceSet<T> = T extends Set<infer X> ? X[] : T
type ReplaceMap<T> = T extends Map<infer K, infer I>
? Record<
K extends string | number | symbol ? K : string,
IsValueType<I> extends true ? I : { [K in keyof ExcludeFuncsFromObj<I>]: Dto<I[K]> }
>
: T
type ReplaceArray<T> = T extends Array<infer X> ? Dto<X>[] : T
type ExcludeFuncsFromObj<T> = Pick<T, { [K in keyof T]: IsFunction<T[K]> extends true ? never : K }[keyof T]>
type Dtoified<T> = IsValueType<T> extends true
? ReplaceDate<ReplaceMap<ReplaceSet<ReplaceArray<T>>>>
: { [K in keyof ExcludeFuncsFromObj<T>]: Dto<T[K]> }
export type Dto<T> = IsFunction<T> extends true
? never
: IsOptional<T> extends true
? Dtoified<Exclude<T, undefined>> | null
: Dtoified<T>
export type Serializable<T> = T & { serialize(): Dto<T> }