@@ -32,28 +32,28 @@ export type BuildOptions = {
32
32
/**
33
33
* The type of the underlying function called on `build()` for objects
34
34
*/
35
- export type BuildingFunction < T > = ( model : Partial < T > , options : BuildOptions ) => T ;
35
+ export type BuildingFunction < TSpec , TBuilt > = ( model : Partial < TSpec > , options : BuildOptions ) => TBuilt ;
36
36
37
37
/**
38
38
* The type of the underlying function called on `build()` for arrays
39
39
*/
40
- export type ArrayBuildingFunction < T > = ( model : Array < T > , options : BuildOptions ) => Array < T > ;
40
+ export type ArrayBuildingFunction < TSpec , TBuilt > = ( model : Array < TSpec > , options : BuildOptions ) => TBuilt ;
41
41
42
42
/**
43
43
* Represents a fluent builder proxy for an object
44
44
*/
45
- export type Builder < T > = {
46
- build : ( option ?: BuildOptions ) => T ;
45
+ export type Builder < TSpec , TBuilt = TSpec > = {
46
+ build : ( option ?: BuildOptions ) => TBuilt ;
47
47
} & {
48
- [ K in keyof T ] -?: ( arg : T [ K ] ) => Builder < T > ;
48
+ [ K in keyof TSpec ] -?: ( arg : TSpec [ K ] ) => Builder < TSpec , TBuilt > ;
49
49
} ;
50
50
51
51
/**
52
52
* Represents a fluent builder proxy for an array
53
53
*/
54
- export type ArrayBuilder < T > = {
55
- push : ( item : T ) => ArrayBuilder < T > ;
56
- build : ( option ?: BuildOptions ) => Array < T > ;
54
+ export type ArrayBuilder < TSpec , TBuilt > = {
55
+ push : ( item : TSpec ) => ArrayBuilder < TSpec , TBuilt > ;
56
+ build : ( option ?: BuildOptions ) => TBuilt ;
57
57
} ;
58
58
59
59
/**
@@ -62,24 +62,27 @@ export type ArrayBuilder<T> = {
62
62
* @param options The build options
63
63
* @returns
64
64
*/
65
- function defaultBuildingFn < T > ( model : Partial < T > , options : BuildOptions ) : T {
65
+ function defaultBuildingFn < TSpec , TBuilt = TSpec > ( model : Partial < TSpec > , options : BuildOptions ) : TBuilt {
66
66
// prevents @typescript -eslint/no-unused-vars ...
67
67
if ( options . validate == null ) {
68
68
options . validate = true ;
69
69
}
70
70
if ( options . normalize == null ) {
71
71
options . normalize = true ;
72
72
}
73
- return model as T ;
73
+ return model as TBuilt ;
74
74
}
75
75
76
76
/**
77
77
* A factory for fluent builders that proxy properties assignations and can validate against schema on build()
78
78
* @param buildingFn The function used to validate and produce the object on build()
79
79
* @returns A fluent builder
80
80
*/
81
- export function builder < T > ( model : Partial < T > = { } , buildingFn : BuildingFunction < T > = defaultBuildingFn ) : Builder < T > {
82
- const proxy = new Proxy ( { } as Builder < T > , {
81
+ export function builder < TSpec , TBuilt = TSpec > (
82
+ model : Partial < TSpec > = { } ,
83
+ buildingFn : BuildingFunction < TSpec , TBuilt > = defaultBuildingFn ,
84
+ ) : Builder < TSpec , TBuilt > {
85
+ const proxy = new Proxy ( { } as Builder < TSpec , TBuilt > , {
83
86
get : ( _ , prop ) => {
84
87
if ( prop === 'build' ) {
85
88
return ( options ?: BuildOptions ) => {
@@ -93,7 +96,7 @@ export function builder<T>(model: Partial<T> = {}, buildingFn: BuildingFunction<
93
96
return buildingFn ( model , options ) ;
94
97
} ;
95
98
}
96
- return ( value : unknown ) : Builder < T > => {
99
+ return ( value : unknown ) : Builder < TSpec , TBuilt > => {
97
100
( model as any ) [ prop . toString ( ) ] = value ;
98
101
return proxy ;
99
102
} ;
@@ -110,14 +113,14 @@ export function builder<T>(model: Partial<T> = {}, buildingFn: BuildingFunction<
110
113
* @param buildingFn The function used to validate and produce the object on build()
111
114
* @returns A fluent builder
112
115
*/
113
- export function arrayBuilder < T > (
114
- model : Array < T > = [ ] ,
115
- buildingFn : ArrayBuildingFunction < T > = defaultBuildingFn ,
116
- ) : ArrayBuilder < T > {
116
+ export function arrayBuilder < TSpec , TBuilt > (
117
+ model : Array < TSpec > = [ ] ,
118
+ buildingFn : ArrayBuildingFunction < TSpec , TBuilt > = defaultBuildingFn ,
119
+ ) : ArrayBuilder < TSpec , TBuilt > {
117
120
if ( model != null && ! Array . isArray ( model ) ) {
118
121
throw new Error ( `The provided model should be an array` ) ;
119
122
}
120
- const proxy = new Proxy ( { } as ArrayBuilder < T > , {
123
+ const proxy = new Proxy ( { } as ArrayBuilder < TSpec , TBuilt > , {
121
124
get : ( _ , prop ) => {
122
125
if ( prop === 'build' ) {
123
126
return ( options ?: BuildOptions ) => {
@@ -132,7 +135,7 @@ export function arrayBuilder<T>(
132
135
} ;
133
136
}
134
137
if ( prop === 'push' ) {
135
- return ( value : T ) : ArrayBuilder < T > => {
138
+ return ( value : TSpec ) : ArrayBuilder < TSpec , TBuilt > => {
136
139
model . push ( value ) ;
137
140
return proxy ;
138
141
} ;
0 commit comments