@@ -41,6 +41,15 @@ export interface AnyCommandConfig {
4141 transforms ?: TransformsConfig < any , any , any , any > ;
4242}
4343
44+ /**
45+ * Command config shape used for type constraints/inference.
46+ *
47+ * Intentionally omits `handler` so inline command handlers are not
48+ * context-typed as `(result: any) => …` before `CommandsInput` can apply the
49+ * computed handler signature.
50+ */
51+ export type AnyCommandConfigInput = Omit < AnyCommandConfig , 'handler' > ;
52+
4453/**
4554 * Array option definition (--flag value --flag value2).
4655 */
@@ -62,7 +71,8 @@ export interface ArrayOption extends OptionBase {
6271export interface BargsConfig <
6372 TOptions extends OptionsSchema = OptionsSchema ,
6473 TPositionals extends PositionalsSchema = PositionalsSchema ,
65- TCommands extends Record < string , AnyCommandConfig > | undefined = undefined ,
74+ TCommands extends Record < string , AnyCommandConfigInput > | undefined =
75+ undefined ,
6676 TTransforms extends TransformsConfig < any , any , any , any > | undefined =
6777 undefined ,
6878> {
@@ -93,17 +103,17 @@ export interface BargsConfig<
93103
94104export type BargsConfigWithCommands <
95105 TOptions extends OptionsSchema = OptionsSchema ,
96- TCommands extends Record < string , AnyCommandConfig > = Record <
106+ TCommands extends Record < string , AnyCommandConfigInput > = Record <
97107 string ,
98- AnyCommandConfig
108+ AnyCommandConfigInput
99109 > ,
100110 TTransforms extends TransformsConfig < any , any , any , any > | undefined =
101111 undefined ,
102112> = Omit <
103113 BargsConfig < TOptions , PositionalsSchema , TCommands , TTransforms > ,
104114 'commands' | 'positionals'
105115> & {
106- commands : CommandsInput < TOptions , TTransforms , TCommands > ;
116+ commands : CommandsInput < TOptions , TTransforms , TCommands > & TCommands ;
107117 defaultHandler ?:
108118 | CommandNames < TCommands >
109119 | Handler <
@@ -123,9 +133,9 @@ export type BargsConfigWithCommands<
123133 */
124134export type BargsConfigWithCommandsInternal <
125135 TOptions extends OptionsSchema = OptionsSchema ,
126- TCommands extends Record < string , AnyCommandConfig > = Record <
136+ TCommands extends Record < string , AnyCommandConfigInput > = Record <
127137 string ,
128- AnyCommandConfig
138+ AnyCommandConfigInput
129139 > ,
130140 TTransforms extends TransformsConfig < any , any , any , any > | undefined =
131141 undefined ,
@@ -400,7 +410,9 @@ export type InferPositionals<T extends PositionalsSchema> = T extends readonly [
400410 ? Only extends PositionalDef
401411 ? readonly [ InferPositional < Only > ]
402412 : readonly [ ]
403- : readonly [ ] ;
413+ : T extends readonly [ ]
414+ ? readonly [ ]
415+ : readonly InferPositional < T [ number ] > [ ] ;
404416
405417/**
406418 * Compute proper handler types for each command in a commands record.
@@ -427,7 +439,7 @@ export type InferPositionals<T extends PositionalsSchema> = T extends readonly [
427439export type InferredCommands <
428440 TGlobalOptions extends OptionsSchema ,
429441 TGlobalTransforms extends TransformsConfig < any , any , any , any > | undefined ,
430- TCommands extends Record < string , AnyCommandConfig > ,
442+ TCommands extends Record < string , AnyCommandConfigInput > ,
431443> = {
432444 [ K in keyof TCommands ] : TCommands [ K ] extends CommandConfig <
433445 infer _TGlobalOpts ,
@@ -445,9 +457,20 @@ export type InferredCommands<
445457 InferCommandResult <
446458 TGlobalOptions ,
447459 TGlobalTransforms ,
448- TCommands [ K ] [ 'options' ] ,
449- TCommands [ K ] [ 'positionals' ] ,
450- TCommands [ K ] [ 'transforms' ]
460+ NonNullable < TCommands [ K ] [ 'options' ] > extends OptionsSchema
461+ ? NonNullable < TCommands [ K ] [ 'options' ] >
462+ : undefined ,
463+ NonNullable < TCommands [ K ] [ 'positionals' ] > extends PositionalsSchema
464+ ? NonNullable < TCommands [ K ] [ 'positionals' ] >
465+ : undefined ,
466+ NonNullable < TCommands [ K ] [ 'transforms' ] > extends TransformsConfig <
467+ any ,
468+ any ,
469+ any ,
470+ any
471+ >
472+ ? NonNullable < TCommands [ K ] [ 'transforms' ] >
473+ : undefined
451474 >
452475 > ;
453476 options ?: TCommands [ K ] [ 'options' ] ;
@@ -620,7 +643,8 @@ interface CommandInput<
620643 * Helper type to extract command names from a commands record for
621644 * defaultHandler typing.
622645 */
623- type CommandNames < T > = T extends Record < infer K , AnyCommandConfig > ? K : never ;
646+ type CommandNames < T > =
647+ T extends Record < infer K , AnyCommandConfigInput > ? K : never ;
624648
625649/**
626650 * Bargs config with commands (requires commands, allows defaultHandler).
@@ -644,7 +668,7 @@ type CommandNames<T> = T extends Record<infer K, AnyCommandConfig> ? K : never;
644668type CommandsInput <
645669 TGlobalOptions extends OptionsSchema ,
646670 TGlobalTransforms extends TransformsConfig < any , any , any , any > | undefined ,
647- TCommands extends Record < string , AnyCommandConfig > ,
671+ TCommands extends Record < string , AnyCommandConfigInput > ,
648672> = {
649673 [ K in keyof TCommands ] : TCommands [ K ] extends CommandConfig <
650674 infer _TGlobalOpts ,
@@ -659,14 +683,19 @@ type CommandsInput<
659683 CommandInput <
660684 TGlobalOptions ,
661685 TGlobalTransforms ,
662- TCommands [ K ] [ 'options' ] extends OptionsSchema
663- ? TCommands [ K ] [ 'options' ]
686+ NonNullable < TCommands [ K ] [ 'options' ] > extends OptionsSchema
687+ ? NonNullable < TCommands [ K ] [ 'options' ] >
664688 : Record < string , never > ,
665- TCommands [ K ] [ 'positionals' ] extends PositionalsSchema
666- ? TCommands [ K ] [ 'positionals' ]
689+ NonNullable < TCommands [ K ] [ 'positionals' ] > extends PositionalsSchema
690+ ? NonNullable < TCommands [ K ] [ 'positionals' ] >
667691 : [ ] ,
668- TCommands [ K ] [ 'transforms' ] extends TransformsConfig < any , any , any , any >
669- ? TCommands [ K ] [ 'transforms' ]
692+ NonNullable < TCommands [ K ] [ 'transforms' ] > extends TransformsConfig <
693+ any ,
694+ any ,
695+ any ,
696+ any
697+ >
698+ ? NonNullable < TCommands [ K ] [ 'transforms' ] >
670699 : undefined
671700 > ;
672701} ;
0 commit comments