Skip to content

Commit de75995

Browse files
authored
fix(command): use Array<unknown> for arguments in .globalAction() action handler (#607)
1 parent 853c2a6 commit de75995

File tree

2 files changed

+56
-8
lines changed

2 files changed

+56
-8
lines changed

command/command.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ export class Command<
800800
public globalAction(
801801
fn: ActionHandler<
802802
Partial<TCommandOptions>,
803-
TCommandArguments,
803+
Array<unknown>,
804804
TCommandGlobals,
805805
TParentCommandGlobals,
806806
TCommandTypes,

command/test/command/action_test.ts

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import {
22
assertEquals,
33
assertSpyCall,
44
assertSpyCalls,
5+
assertType,
6+
IsExact,
57
spy,
68
} from "../../../dev_deps.ts";
79
import { Command } from "../../command.ts";
@@ -79,25 +81,71 @@ Deno.test("[flags] should call global action handler", async () => {
7981
const barGlobalSpy = spy();
8082
const barSpy = spy();
8183

82-
const barCmd = new Command()
84+
const barCmd = new Command<{ main?: true; foo?: true }>()
8385
.globalOption("--bar", "...")
8486
.option("--baz", "...")
8587
.arguments("<foo> <bar>")
86-
.globalAction(barGlobalSpy)
88+
.globalAction((opts, ...args) => {
89+
assertType<
90+
IsExact<typeof opts, {
91+
main?: true;
92+
foo?: true;
93+
bar?: true;
94+
baz?: true;
95+
}>
96+
>(true);
97+
assertType<IsExact<typeof args, Array<unknown>>>(true);
98+
barGlobalSpy(opts, ...args);
99+
})
87100
.action(barSpy);
88101

89-
const fooCmd = new Command()
102+
const fooCmd = new Command<{ main?: true }>()
90103
.globalOption("--foo", "...")
91-
.globalAction(fooGlobalSpy)
104+
.globalAction((opts, ...args) => {
105+
assertType<
106+
IsExact<typeof opts, {
107+
main?: true;
108+
foo?: true;
109+
}>
110+
>(true);
111+
assertType<IsExact<typeof args, Array<unknown>>>(true);
112+
fooGlobalSpy(opts, ...args);
113+
})
92114
.action(fooSpy)
93115
.command("bar", barCmd);
94116

95117
const cmd = new Command()
96118
.throwErrors()
97119
.globalOption("--main", "...")
98-
.globalAction(mainGlobalSpy)
99-
.action(mainSpy)
100-
.command("foo", fooCmd);
120+
.option("--test", "...")
121+
.globalAction((opts, ...args) => {
122+
assertType<
123+
IsExact<typeof opts, {
124+
main?: true;
125+
test?: true;
126+
}>
127+
>(true);
128+
assertType<IsExact<typeof args, Array<unknown>>>(true);
129+
mainGlobalSpy(opts, ...args);
130+
})
131+
.action((opts, ...args) => {
132+
mainSpy(opts, ...args);
133+
assertType<
134+
IsExact<typeof opts, {
135+
main?: true;
136+
test?: true;
137+
}>
138+
>(true);
139+
})
140+
.command("foo", fooCmd)
141+
.command("other", "desc...")
142+
.action((opts) => {
143+
assertType<
144+
IsExact<typeof opts, {
145+
main?: true;
146+
}>
147+
>(true);
148+
});
101149

102150
await cmd.parse([
103151
"--main",

0 commit comments

Comments
 (0)