Skip to content

Commit 1b57ba3

Browse files
authored
fix: fix generic type issues (#38)
1 parent d181698 commit 1b57ba3

File tree

5 files changed

+28
-16
lines changed

5 files changed

+28
-16
lines changed

src/args.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import { parseRawArgs } from "./_parser";
33
import type { Arg, ArgsDef, ParsedArgs } from "./types";
44
import { CLIError, toArray } from "./_utils";
55

6-
export function parseArgs(rawArgs: string[], argsDef: ArgsDef): ParsedArgs {
6+
export function parseArgs<T extends ArgsDef = ArgsDef>(
7+
rawArgs: string[],
8+
argsDef: ArgsDef
9+
): ParsedArgs<T> {
710
const parseOptions = {
811
boolean: [] as string[],
912
string: [] as string[],
@@ -58,7 +61,7 @@ export function parseArgs(rawArgs: string[], argsDef: ArgsDef): ParsedArgs {
5861
}
5962
}
6063

61-
return parsedArgsProxy;
64+
return parsedArgsProxy as ParsedArgs<T>;
6265
}
6366

6467
export function resolveArgs(argsDef: ArgsDef): Arg[] {

src/command.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ export interface RunCommandOptions {
1313
showUsage?: boolean;
1414
}
1515

16-
export async function runCommand(
17-
cmd: CommandDef,
16+
export async function runCommand<T extends ArgsDef = ArgsDef>(
17+
cmd: CommandDef<T>,
1818
opts: RunCommandOptions
1919
): Promise<void> {
2020
const cmdArgs = await resolveValue(cmd.args || {});
21-
const parsedArgs = parseArgs(opts.rawArgs, cmdArgs);
21+
const parsedArgs = parseArgs<T>(opts.rawArgs, cmdArgs);
2222

23-
const context: CommandContext = {
23+
const context: CommandContext<T> = {
2424
rawArgs: opts.rawArgs,
2525
args: parsedArgs,
2626
cmd,
@@ -64,11 +64,11 @@ export async function runCommand(
6464
}
6565
}
6666

67-
export async function resolveSubCommand(
68-
cmd: CommandDef,
67+
export async function resolveSubCommand<T extends ArgsDef = ArgsDef>(
68+
cmd: CommandDef<T>,
6969
rawArgs: string[],
70-
parent?: CommandDef
71-
): Promise<[CommandDef, CommandDef?]> {
70+
parent?: CommandDef<T>
71+
): Promise<[CommandDef<T>, CommandDef<T>?]> {
7272
const subCommands = await resolveValue(cmd.subCommands);
7373
if (subCommands && Object.keys(subCommands).length > 0) {
7474
const subCommandArgIndex = rawArgs.findIndex((arg) => !arg.startsWith("-"));

src/main.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { bgRed } from "colorette";
2-
import type { CommandDef } from "./types";
2+
import type { ArgsDef, CommandDef } from "./types";
33
import { resolveSubCommand, runCommand } from "./command";
44
import { CLIError } from "./_utils";
55
import { showUsage } from "./usage";
@@ -8,7 +8,10 @@ export interface RunMainOptions {
88
rawArgs?: string[];
99
}
1010

11-
export async function runMain(cmd: CommandDef, opts: RunMainOptions = {}) {
11+
export async function runMain<T extends ArgsDef = ArgsDef>(
12+
cmd: CommandDef<T>,
13+
opts: RunMainOptions = {}
14+
) {
1215
const rawArgs = opts.rawArgs || process.argv.slice(2);
1316
try {
1417
if (rawArgs.includes("--help") || rawArgs.includes("-h")) {

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export type CommandDef<T extends ArgsDef = ArgsDef> = {
6262
export type CommandContext<T extends ArgsDef = ArgsDef> = {
6363
rawArgs: string[];
6464
args: ParsedArgs<T>;
65-
cmd: CommandDef;
65+
cmd: CommandDef<T>;
6666
subCommand?: CommandDef<T>;
6767
};
6868

src/usage.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
import { formatLineColumns, resolveValue } from "./_utils";
2-
import type { CommandDef } from "./types";
2+
import type { ArgsDef, CommandDef } from "./types";
33
import { resolveArgs } from "./args";
44

5-
export async function showUsage(cmd: CommandDef, parent?: CommandDef) {
5+
export async function showUsage<T extends ArgsDef = ArgsDef>(
6+
cmd: CommandDef<T>,
7+
parent?: CommandDef<T>
8+
) {
69
try {
710
console.log((await renderUsage(cmd, parent)) + "\n");
811
} catch (error) {
912
console.error(error);
1013
}
1114
}
1215

13-
export async function renderUsage(cmd: CommandDef, parent?: CommandDef) {
16+
export async function renderUsage<T extends ArgsDef = ArgsDef>(
17+
cmd: CommandDef<T>,
18+
parent?: CommandDef<T>
19+
) {
1420
const cmdMeta = await resolveValue(cmd.meta || {});
1521
const cmdArgs = resolveArgs(await resolveValue(cmd.args || {}));
1622
const parentMeta = await resolveValue(parent?.meta || {});

0 commit comments

Comments
 (0)