-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
60 lines (49 loc) · 1.42 KB
/
index.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
57
58
59
60
interface Some {
status: string[];
test: number;
}
interface User {
status: string;
test: number;
}
interface Message {
id: string;
user: User;
date: Date;
hello: string;
}
interface Person {
name: string;
age: number;
address: {
street: string;
city: string;
messages: Message[];
};
}
/** override types for all fields */
type OverrideTypes<T, NewType> = {
[K in keyof T]: T[K] extends object ? OverrideTypes<T[K], NewType> : NewType;
};
const over: OverrideTypes<Some, Date> = {
status: [new Date()],
test: new Date(),
};
/** output fields protocol */
type IncludedFields<Entity, Prefix extends string = ""> = {
[Key in keyof Entity]: Key extends string | number
? Entity[Key] extends (infer U)[]
? `${Prefix}${Key}` | `${IncludedFields<U, `${Prefix}${Key}.`>}`
: Entity[Key] extends object
? Entity[Key] extends (...arg: any) => any
? never
: `${Prefix}${Key}` | `${IncludedFields<Entity[Key], `${Prefix}${Key}.`>}`
: `${Prefix}${Key}`
: never;
}[keyof Entity];
type GetArrayType<T> = T extends (infer U)[] ? U : never;
type SomeT<T> = T extends string | number ? T : never;
/** @example "-message.id" */
type ExcludedFields<T> = `-${IncludedFields<T>}`;
type OutputFields<T> = ExcludedFields<T>[] | IncludedFields<T>[]
const some: OutputFields<Person> = ["address.messages.user.status", "address.messages.user.status"];