@@ -12,6 +12,8 @@ import type {
12
12
TaskOverrideDefinition ,
13
13
EmptyTaskDefinitionBuilder ,
14
14
EmptyTaskDefinition ,
15
+ ExtendTaskArguments ,
16
+ TaskArguments ,
15
17
} from "../../types/tasks.js" ;
16
18
17
19
import { HardhatError } from "@ignored/hardhat-vnext-errors" ;
@@ -55,8 +57,9 @@ export class EmptyTaskDefinitionBuilderImplementation
55
57
}
56
58
}
57
59
58
- export class NewTaskDefinitionBuilderImplementation
59
- implements NewTaskDefinitionBuilder
60
+ export class NewTaskDefinitionBuilderImplementation <
61
+ TaskArgumentsT extends TaskArguments = TaskArguments ,
62
+ > implements NewTaskDefinitionBuilder < TaskArgumentsT >
60
63
{
61
64
readonly #id: string [ ] ;
62
65
readonly #usedNames: Set < string > = new Set ( ) ;
@@ -66,7 +69,7 @@ export class NewTaskDefinitionBuilderImplementation
66
69
67
70
#description: string ;
68
71
69
- #action?: NewTaskActionFunction | string ;
72
+ #action?: NewTaskActionFunction < TaskArgumentsT > | string ;
70
73
71
74
constructor ( id : string | string [ ] , description : string = "" ) {
72
75
validateId ( id ) ;
@@ -80,25 +83,32 @@ export class NewTaskDefinitionBuilderImplementation
80
83
return this ;
81
84
}
82
85
83
- public setAction ( action : NewTaskActionFunction | string ) : this {
86
+ public setAction (
87
+ action : NewTaskActionFunction < TaskArgumentsT > | string ,
88
+ ) : this {
84
89
validateAction ( action ) ;
85
90
86
91
this . #action = action ;
87
92
88
93
return this ;
89
94
}
90
95
91
- public addOption < T extends ArgumentType > ( {
96
+ public addOption <
97
+ NameT extends string ,
98
+ TypeT extends ArgumentType = ArgumentType . STRING ,
99
+ > ( {
92
100
name,
93
101
description = "" ,
94
102
type,
95
103
defaultValue,
96
104
} : {
97
- name : string ;
105
+ name : NameT ;
98
106
description ?: string ;
99
- type ?: T ;
100
- defaultValue : ArgumentTypeToValueType < T > ;
101
- } ) : this {
107
+ type ?: TypeT ;
108
+ defaultValue : ArgumentTypeToValueType < TypeT > ;
109
+ } ) : NewTaskDefinitionBuilder <
110
+ ExtendTaskArguments < NameT , TypeT , TaskArgumentsT >
111
+ > {
102
112
const argumentType = type ?? ArgumentType . STRING ;
103
113
104
114
const optionDefinition = {
@@ -115,32 +125,47 @@ export class NewTaskDefinitionBuilderImplementation
115
125
return this ;
116
126
}
117
127
118
- public addFlag ( flagConfig : { name : string ; description ?: string } ) : this {
128
+ public addFlag < NameT extends string > ( flagConfig : {
129
+ name : NameT ;
130
+ description ?: string ;
131
+ } ) : NewTaskDefinitionBuilder <
132
+ ExtendTaskArguments < NameT , ArgumentType . BOOLEAN , TaskArgumentsT >
133
+ > {
119
134
return this . addOption ( {
120
135
...flagConfig ,
121
136
type : ArgumentType . BOOLEAN ,
122
137
defaultValue : false ,
123
138
} ) ;
124
139
}
125
140
126
- public addPositionalArgument < T extends ArgumentType > ( argConfig : {
127
- name : string ;
141
+ public addPositionalArgument <
142
+ NameT extends string ,
143
+ TypeT extends ArgumentType = ArgumentType . STRING ,
144
+ > ( argConfig : {
145
+ name : NameT ;
128
146
description ?: string ;
129
- type ?: T ;
130
- defaultValue ?: ArgumentTypeToValueType < T > ;
131
- } ) : this {
147
+ type ?: TypeT ;
148
+ defaultValue ?: ArgumentTypeToValueType < TypeT > ;
149
+ } ) : NewTaskDefinitionBuilder <
150
+ ExtendTaskArguments < NameT , TypeT , TaskArgumentsT >
151
+ > {
132
152
return this . #addPositionalArgument( {
133
153
...argConfig ,
134
154
isVariadic : false ,
135
155
} ) ;
136
156
}
137
157
138
- public addVariadicArgument < T extends ArgumentType > ( argConfig : {
139
- name : string ;
158
+ public addVariadicArgument <
159
+ NameT extends string ,
160
+ TypeT extends ArgumentType = ArgumentType . STRING ,
161
+ > ( argConfig : {
162
+ name : NameT ;
140
163
description ?: string ;
141
- type ?: T ;
142
- defaultValue ?: Array < ArgumentTypeToValueType < T > > ;
143
- } ) : this {
164
+ type ?: TypeT ;
165
+ defaultValue ?: Array < ArgumentTypeToValueType < TypeT > > ;
166
+ } ) : NewTaskDefinitionBuilder <
167
+ ExtendTaskArguments < NameT , TypeT , TaskArgumentsT >
168
+ > {
144
169
return this . #addPositionalArgument( {
145
170
...argConfig ,
146
171
isVariadic : true ,
@@ -158,27 +183,36 @@ export class NewTaskDefinitionBuilderImplementation
158
183
type : TaskDefinitionType . NEW_TASK ,
159
184
id : this . #id,
160
185
description : this . #description,
161
- action : this . #action,
186
+ /* eslint-disable-next-line @typescript-eslint/consistent-type-assertions
187
+ -- The type of the action is narrowed in the setAction function to
188
+ improve the argument types. Once the task is built, we use the more
189
+ general type to avoid having to parameterize the NewTaskDefinition */
190
+ action : this . #action as NewTaskActionFunction ,
162
191
options : this . #options,
163
192
positionalArguments : this . #positionalArgs,
164
193
} ;
165
194
}
166
195
167
- #addPositionalArgument< T extends ArgumentType > ( {
196
+ #addPositionalArgument<
197
+ NameT extends string ,
198
+ TypeT extends ArgumentType = ArgumentType . STRING ,
199
+ > ( {
168
200
name,
169
201
description = "" ,
170
202
type,
171
203
defaultValue,
172
204
isVariadic,
173
205
} : {
174
- name : string ;
206
+ name : NameT ;
175
207
description ?: string ;
176
- type ?: T ;
208
+ type ?: TypeT ;
177
209
defaultValue ?:
178
- | ArgumentTypeToValueType < T >
179
- | Array < ArgumentTypeToValueType < T > > ;
210
+ | ArgumentTypeToValueType < TypeT >
211
+ | Array < ArgumentTypeToValueType < TypeT > > ;
180
212
isVariadic : boolean ;
181
- } ) : this {
213
+ } ) : NewTaskDefinitionBuilder <
214
+ ExtendTaskArguments < NameT , TypeT , TaskArgumentsT >
215
+ > {
182
216
const argumentType = type ?? ArgumentType . STRING ;
183
217
184
218
const positionalArgDef = {
@@ -203,16 +237,17 @@ export class NewTaskDefinitionBuilderImplementation
203
237
}
204
238
}
205
239
206
- export class TaskOverrideDefinitionBuilderImplementation
207
- implements TaskOverrideDefinitionBuilder
240
+ export class TaskOverrideDefinitionBuilderImplementation <
241
+ TaskArgumentsT extends TaskArguments = TaskArguments ,
242
+ > implements TaskOverrideDefinitionBuilder < TaskArgumentsT >
208
243
{
209
244
readonly #id: string [ ] ;
210
245
211
246
readonly #options: Record < string , OptionDefinition > = { } ;
212
247
213
248
#description?: string ;
214
249
215
- #action?: TaskOverrideActionFunction | string ;
250
+ #action?: TaskOverrideActionFunction < TaskArgumentsT > | string ;
216
251
217
252
constructor ( id : string | string [ ] ) {
218
253
validateId ( id ) ;
@@ -225,25 +260,32 @@ export class TaskOverrideDefinitionBuilderImplementation
225
260
return this ;
226
261
}
227
262
228
- public setAction ( action : TaskOverrideActionFunction | string ) : this {
263
+ public setAction (
264
+ action : TaskOverrideActionFunction < TaskArgumentsT > | string ,
265
+ ) : this {
229
266
validateAction ( action ) ;
230
267
231
268
this . #action = action ;
232
269
233
270
return this ;
234
271
}
235
272
236
- public addOption < T extends ArgumentType > ( {
273
+ public addOption <
274
+ NameT extends string ,
275
+ TypeT extends ArgumentType = ArgumentType . STRING ,
276
+ > ( {
237
277
name,
238
278
description = "" ,
239
279
type,
240
280
defaultValue,
241
281
} : {
242
- name : string ;
282
+ name : NameT ;
243
283
description ?: string ;
244
- type ?: T ;
245
- defaultValue : ArgumentTypeToValueType < T > ;
246
- } ) : this {
284
+ type ?: TypeT ;
285
+ defaultValue : ArgumentTypeToValueType < TypeT > ;
286
+ } ) : TaskOverrideDefinitionBuilder <
287
+ ExtendTaskArguments < NameT , TypeT , TaskArgumentsT >
288
+ > {
247
289
const argumentType = type ?? ArgumentType . STRING ;
248
290
249
291
const optionDefinition = {
@@ -264,7 +306,12 @@ export class TaskOverrideDefinitionBuilderImplementation
264
306
return this ;
265
307
}
266
308
267
- public addFlag ( flagConfig : { name : string ; description ?: string } ) : this {
309
+ public addFlag < NameT extends string > ( flagConfig : {
310
+ name : string ;
311
+ description ?: string ;
312
+ } ) : TaskOverrideDefinitionBuilder <
313
+ ExtendTaskArguments < NameT , ArgumentType . BOOLEAN , TaskArgumentsT >
314
+ > {
268
315
return this . addOption ( {
269
316
...flagConfig ,
270
317
type : ArgumentType . BOOLEAN ,
@@ -283,7 +330,11 @@ export class TaskOverrideDefinitionBuilderImplementation
283
330
type : TaskDefinitionType . TASK_OVERRIDE ,
284
331
id : this . #id,
285
332
description : this . #description,
286
- action : this . #action,
333
+ /* eslint-disable-next-line @typescript-eslint/consistent-type-assertions
334
+ -- The type of the action is narrowed in the setAction function to
335
+ improve the argument types. Once the task is built, we use the more
336
+ general type to avoid having to parameterize the TaskOverrideDefinition */
337
+ action : this . #action as TaskOverrideActionFunction ,
287
338
options : this . #options,
288
339
} ;
289
340
}
0 commit comments