@@ -17,7 +17,23 @@ export type PositionalArgDef = Omit<_ArgDef<"positional", string>, "alias">;
17
17
export type ArgDef = BooleanArgDef | StringArgDef | PositionalArgDef ;
18
18
export type ArgsDef = Record < string , ArgDef > ;
19
19
export type Arg = ArgDef & { name : string ; alias : string [ ] } ;
20
- export type ParsedArgs = Record < string , string | boolean > & { _ : string [ ] } ;
20
+
21
+ export type ParsedArgs < T extends ArgsDef = ArgsDef > = { _ : string [ ] } & Record <
22
+ { [ K in keyof T ] : T [ K ] extends { type : "positional" } ? K : never } [ keyof T ] ,
23
+ string | boolean
24
+ > &
25
+ Record <
26
+ {
27
+ [ K in keyof T ] : T [ K ] extends { type : "string" } ? K : never ;
28
+ } [ keyof T ] ,
29
+ string
30
+ > &
31
+ Record <
32
+ {
33
+ [ K in keyof T ] : T [ K ] extends { type : "boolean" } ? K : never ;
34
+ } [ keyof T ] ,
35
+ boolean
36
+ > ;
21
37
22
38
// ----- Command -----
23
39
@@ -33,21 +49,21 @@ export interface CommandMeta {
33
49
34
50
export type SubCommandsDef = Record < string , Resolvable < CommandDef > > ;
35
51
36
- export type CommandDef = {
52
+ export type CommandDef < T extends ArgsDef = ArgsDef > = {
37
53
meta ?: Resolvable < CommandMeta > ;
38
- args ?: Resolvable < ArgsDef > ;
54
+ args ?: Resolvable < T > ;
39
55
subCommands ?: Resolvable < SubCommandsDef > ;
40
- setup ?: ( context : CommandContext ) => any | Promise < any > ;
41
- cleanup ?: ( context : CommandContext ) => any | Promise < any > ;
42
- run ?: ( context : CommandContext ) => any | Promise < any > ;
56
+ setup ?: ( context : CommandContext < T > ) => any | Promise < any > ;
57
+ cleanup ?: ( context : CommandContext < T > ) => any | Promise < any > ;
58
+ run ?: ( context : CommandContext < T > ) => any | Promise < any > ;
43
59
} ;
44
60
45
- export interface CommandContext {
61
+ export type CommandContext < T extends ArgsDef = ArgsDef > = {
46
62
rawArgs : string [ ] ;
47
- args : ParsedArgs ;
63
+ args : ParsedArgs < T > ;
48
64
cmd : CommandDef ;
49
- subCommand ?: CommandDef ;
50
- }
65
+ subCommand ?: CommandDef < T > ;
66
+ } ;
51
67
52
68
// ----- Utils -----
53
69
0 commit comments