-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathincludeProperties.ts
More file actions
83 lines (79 loc) · 2.38 KB
/
includeProperties.ts
File metadata and controls
83 lines (79 loc) · 2.38 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { ArrayType, CheckCircularly, Primitive } from "./utility-types";
/**
* Join is a utility type that concatenates two property keys with a dot.
* It's used to generate the paths of nested properties.
*/
type Join<K, P> = K extends string | number
? P extends string | number
? `${K}.${P}`
: never
: never;
/**
* GetNonPrimitive is a mapped type that iterates over the properties of a type.
* If a property's type is a primitive, it's excluded from the resulting type.
* If a property's type is the same as the parent type, it's also excluded to prevent circular references.
*/
export type GetNonPrimitive<Type, Parent = never> = [Type] extends [Primitive | []] ? never : {
[Property in keyof Type as Type[Property] extends Primitive | [] ? never : Property]-?:
Type[Property] extends (infer U)[]
? CheckCircularly<U, Parent>
: CheckCircularly<Type[Property], Parent>;
};
/**
* Paths is a recursive type that generates a union of the paths of all non-primitive properties in a type.
* The paths are represented as strings, with nested properties separated by dots.
*/
type Paths<T, Parent = never> = T extends Primitive
? never
: {
[K in keyof GetNonPrimitive<T, Parent>]-?: K extends string | number
? `${K}` | (GetNonPrimitive<T, Parent>[K] extends (infer U)[]
? Join<K, Paths<U, T>>
: Join<K, Paths<GetNonPrimitive<T, Parent>[K], T>>)
: never;
}[keyof GetNonPrimitive<T, Parent>] extends infer Result
? Result extends string | number
? Result
: never
: never;
/**
* Represents a type that includes only DTO type. Useful for .NET developers, it's like an Include method.
* Sub type is separated by a .
*
* @usageNotes
*
* ### [EntityA]
* ````
* export interface EntityA {
* PropertyPrimitve1: number;
* PropertyPrimitve2: Date;
* PropertyPrimitve3?: TimeSpan;
* EntityB: EntityB;
* }
* ````
* ### [EntityB]
* ````
* export interface EntityB {
* PropertyPrimitve1: number;
* PropertyPrimitve2: Date;
* PropertyPrimitve3?: TimeSpan;
* EntityC: EntityC;
* }
* ````
* ### [EntityC]
* ````
* export interface EntityC {
* PropertyPrimitve1: number;
* PropertyPrimitve2: Date;
* PropertyPrimitve3?: TimeSpan;
* }
* ````
* ### [Expected]
*
* ```
* type IncludeResult = IncludeProperties<EntityA>;
* ```
* // ^? "EntityB" | "EntityB.EntityC"
*
**/
export type IncludeProperties<T> = Paths<GetNonPrimitive<ArrayType<T>>>;