@@ -6,30 +6,125 @@ import { barPlugin } from "./plugins/bar/index.js";
6
6
import { voidPlugin } from "./plugins/void/index.js" ;
7
7
import { withOptionsPlugin } from "./plugins/with-options" ;
8
8
9
- const base = new Base ( ) ;
9
+ const base = new Base ( {
10
+ version : "1.2.3" ,
11
+ } ) ;
10
12
11
13
// @ts -expect-error unknown properties cannot be used, see #31
12
14
base . unknown ;
13
15
14
- const FooBase = Base . plugin ( fooPlugin ) . defaults ( {
15
- default : "value" ,
16
+ const BaseWithEmptyDefaults = Base . defaults ( {
17
+ // there should be no required options
18
+ } ) ;
19
+
20
+ // 'version' is missing and should still be required
21
+ // @ts -expect-error
22
+ new BaseWithEmptyDefaults ( )
23
+
24
+ // 'version' is missing and should still be required
25
+ // @ts -expect-error
26
+ new BaseWithEmptyDefaults ( { } )
27
+
28
+ const BaseLevelOne = Base . plugin ( fooPlugin ) . defaults ( {
29
+ defaultOne : "value" ,
30
+ version : "1.2.3" ,
31
+ } ) ;
32
+
33
+ // Because 'version' is already provided, this needs no argument
34
+ new BaseLevelOne ( ) ;
35
+ new BaseLevelOne ( { } ) ;
36
+
37
+ expectType < {
38
+ defaultOne : string ,
39
+ version : string ,
40
+ } > ( BaseLevelOne . defaultOptions ) ;
41
+
42
+ const baseLevelOne = new BaseLevelOne ( {
43
+ optionOne : "value" ,
16
44
} ) ;
17
- const fooBase = new FooBase ( {
18
- option : "value" ,
45
+
46
+ expectType < string > ( baseLevelOne . options . defaultOne ) ;
47
+ expectType < string > ( baseLevelOne . options . optionOne ) ;
48
+ expectType < string > ( baseLevelOne . options . version ) ;
49
+ // @ts -expect-error unknown properties cannot be used, see #31
50
+ baseLevelOne . unknown ;
51
+
52
+ const BaseLevelTwo = BaseLevelOne . defaults ( {
53
+ defaultTwo : 0 ,
19
54
} ) ;
20
55
21
- expectType < string > ( fooBase . options . default ) ;
22
- expectType < string > ( fooBase . options . option ) ;
23
- expectType < string > ( fooBase . foo ) ;
56
+ expectType < {
57
+ defaultOne : string ,
58
+ defaultTwo : number ,
59
+ version : string ,
60
+ } > ( { ...BaseLevelTwo . defaultOptions } ) ;
61
+
62
+ // Because 'version' is already provided, this needs no argument
63
+ new BaseLevelTwo ( ) ;
64
+ new BaseLevelTwo ( { } ) ;
65
+
66
+ // 'version' may be overriden, though it's not necessary
67
+ new BaseLevelTwo ( {
68
+ version : 'new version' ,
69
+ } ) ;
70
+
71
+ const baseLevelTwo = new BaseLevelTwo ( {
72
+ optionTwo : true
73
+ } ) ;
74
+
75
+ expectType < number > ( baseLevelTwo . options . defaultTwo ) ;
76
+ expectType < string > ( baseLevelTwo . options . defaultOne ) ;
77
+ expectType < boolean > ( baseLevelTwo . options . optionTwo ) ;
78
+ expectType < string > ( baseLevelTwo . options . version ) ;
79
+ // @ts -expect-error unknown properties cannot be used, see #31
80
+ baseLevelTwo . unknown ;
81
+
82
+ const BaseLevelThree = BaseLevelTwo . defaults ( {
83
+ defaultThree : [ 'a' , 'b' , 'c' ] ,
84
+ } ) ;
85
+
86
+ expectType < {
87
+ defaultOne : string ,
88
+ defaultTwo : number ,
89
+ defaultThree : string [ ] ,
90
+ version : string ,
91
+ } > ( { ...BaseLevelThree . defaultOptions } ) ;
92
+
93
+ // Because 'version' is already provided, this needs no argument
94
+ new BaseLevelThree ( ) ;
95
+ new BaseLevelThree ( { } ) ;
96
+
97
+ // Previous settings may be overriden, though it's not necessary
98
+ new BaseLevelThree ( {
99
+ optionOne : '' ,
100
+ optionTwo : false ,
101
+ version : 'new version' ,
102
+ } ) ;
103
+
104
+ const baseLevelThree = new BaseLevelThree ( {
105
+ optionThree : [ 0 , 1 , 2 ]
106
+ } ) ;
107
+
108
+ expectType < string > ( baseLevelThree . options . defaultOne ) ;
109
+ expectType < number > ( baseLevelThree . options . defaultTwo ) ;
110
+ expectType < string [ ] > ( baseLevelThree . options . defaultThree ) ;
111
+ expectType < number [ ] > ( baseLevelThree . options . optionThree ) ;
112
+ expectType < string > ( baseLevelThree . options . version ) ;
113
+ // @ts -expect-error unknown properties cannot be used, see #31
114
+ baseLevelThree . unknown ;
24
115
25
116
const BaseWithVoidPlugin = Base . plugin ( voidPlugin ) ;
26
- const baseWithVoidPlugin = new BaseWithVoidPlugin ( ) ;
117
+ const baseWithVoidPlugin = new BaseWithVoidPlugin ( {
118
+ version : "1.2.3" ,
119
+ } ) ;
27
120
28
121
// @ts -expect-error unknown properties cannot be used, see #31
29
122
baseWithVoidPlugin . unknown ;
30
123
31
124
const BaseWithFooAndBarPlugins = Base . plugin ( barPlugin , fooPlugin ) ;
32
- const baseWithFooAndBarPlugins = new BaseWithFooAndBarPlugins ( ) ;
125
+ const baseWithFooAndBarPlugins = new BaseWithFooAndBarPlugins ( {
126
+ version : "1.2.3" ,
127
+ } ) ;
33
128
34
129
expectType < string > ( baseWithFooAndBarPlugins . foo ) ;
35
130
expectType < string > ( baseWithFooAndBarPlugins . bar ) ;
@@ -42,7 +137,9 @@ const BaseWithVoidAndNonVoidPlugins = Base.plugin(
42
137
voidPlugin ,
43
138
fooPlugin
44
139
) ;
45
- const baseWithVoidAndNonVoidPlugins = new BaseWithVoidAndNonVoidPlugins ( ) ;
140
+ const baseWithVoidAndNonVoidPlugins = new BaseWithVoidAndNonVoidPlugins ( {
141
+ version : "1.2.3" ,
142
+ } ) ;
46
143
47
144
expectType < string > ( baseWithVoidAndNonVoidPlugins . foo ) ;
48
145
expectType < string > ( baseWithVoidAndNonVoidPlugins . bar ) ;
@@ -51,6 +148,83 @@ expectType<string>(baseWithVoidAndNonVoidPlugins.bar);
51
148
baseWithVoidAndNonVoidPlugins . unknown ;
52
149
53
150
const BaseWithOptionsPlugin = Base . plugin ( withOptionsPlugin ) ;
54
- const baseWithOptionsPlugin = new BaseWithOptionsPlugin ( ) ;
151
+ const baseWithOptionsPlugin = new BaseWithOptionsPlugin ( {
152
+ version : "1.2.3" ,
153
+ } ) ;
55
154
56
155
expectType < string > ( baseWithOptionsPlugin . getFooOption ( ) ) ;
156
+
157
+ // Test depth limits of `.defaults()` chaining
158
+ const BaseLevelFour = BaseLevelThree . defaults ( { defaultFour : 4 } ) ;
159
+
160
+ expectType < {
161
+ version : string ;
162
+ defaultOne : string ;
163
+ defaultTwo : number ;
164
+ defaultThree : string [ ] ;
165
+ defaultFour : number ;
166
+ } > ( { ...BaseLevelFour . defaultOptions } ) ;
167
+
168
+ const baseLevelFour = new BaseLevelFour ( ) ;
169
+
170
+ // See the node on static defaults in index.d.ts for why defaultFour is missing
171
+ // .options from .defaults() is only supported until a depth of 4
172
+ expectType < {
173
+ version : string ;
174
+ defaultOne : string ;
175
+ defaultTwo : number ;
176
+ defaultThree : string [ ] ;
177
+ } > ( { ...baseLevelFour . options } ) ;
178
+
179
+ expectType < {
180
+ version : string ;
181
+ defaultOne : string ;
182
+ defaultTwo : number ;
183
+ defaultThree : string [ ] ;
184
+ defaultFour : number ;
185
+ // @ts -expect-error - .options from .defaults() is only supported until a depth of 4
186
+ } > ( { ...baseLevelFour . options } ) ;
187
+
188
+ const BaseWithChainedDefaultsAndPlugins = Base
189
+ . defaults ( {
190
+ defaultOne : "value" ,
191
+ } )
192
+ . plugin ( fooPlugin )
193
+ . defaults ( {
194
+ defaultTwo : 0 ,
195
+ } ) ;
196
+
197
+ const baseWithChainedDefaultsAndPlugins =
198
+ new BaseWithChainedDefaultsAndPlugins ( {
199
+ version : "1.2.3" ,
200
+ } ) ;
201
+
202
+ expectType < string > ( baseWithChainedDefaultsAndPlugins . foo ) ;
203
+
204
+ const BaseWithManyChainedDefaultsAndPlugins = Base . defaults ( {
205
+ defaultOne : "value" ,
206
+ } )
207
+ . plugin ( fooPlugin , barPlugin , voidPlugin )
208
+ . defaults ( {
209
+ defaultTwo : 0 ,
210
+ } )
211
+ . plugin ( withOptionsPlugin )
212
+ . defaults ( {
213
+ defaultThree : [ "a" , "b" , "c" ] ,
214
+ } ) ;
215
+
216
+ expectType < {
217
+ defaultOne : string ;
218
+ defaultTwo : number ;
219
+ defaultThree : string [ ] ;
220
+ } > ( { ...BaseWithManyChainedDefaultsAndPlugins . defaultOptions } ) ;
221
+
222
+ const baseWithManyChainedDefaultsAndPlugins =
223
+ new BaseWithManyChainedDefaultsAndPlugins ( {
224
+ version : "1.2.3" ,
225
+ foo : "bar" ,
226
+ } ) ;
227
+
228
+ expectType < string > ( baseWithManyChainedDefaultsAndPlugins . foo ) ;
229
+ expectType < string > ( baseWithManyChainedDefaultsAndPlugins . bar ) ;
230
+ expectType < string > ( baseWithManyChainedDefaultsAndPlugins . getFooOption ( ) ) ;
0 commit comments