remove `any` types`
vi did:web:vt3e.cat
Sun, 10 May 2026 01:52:10 +0100
1 files changed,
50 insertions(+),
15 deletions(-)
jump to
M
src/index.ts
→
src/index.ts
@@ -1,12 +1,15 @@
import type { ArgumentOptions, FlagOptions, Prettify } from "./types"; -export class Command<TContext extends Record<string, any> = {}> { +export class Command<TContext extends Record<string, unknown> = {}> { public name?: string; public description?: string; private flags: Record<string, FlagOptions> = {}; - private args: Record<string, ArgumentOptions<any>> = {}; - private positionals: Array<{ name: string; options: any }> = []; - private subcommands: Record<string, Command<any>> = {}; + private args: Record<string, ArgumentOptions<unknown>> = {}; + private positionals: Array<{ + name: string; + options: { description?: string; required?: boolean } | undefined; + }> = []; + private subcommands: Record<string, Command<Record<string, unknown>>> = {}; private actionFn?: (ctx: TContext) => void | Promise<void>; constructor(name?: string) {@@ -28,7 +31,9 @@ name: TName,
options?: FlagOptions, ): Command<Prettify<TContext & { [K in TName]: boolean }>> { this.flags[name] = options || {}; - return this; + return this as unknown as Command< + Prettify<TContext & { [K in TName]: boolean }> + >; } addStringArgument<TName extends string, TReq extends boolean = false>(@@ -42,7 +47,13 @@ }
> > { this.args[name] = { ...options, type: "string" }; - return this; + return this as unknown as Command< + Prettify< + TContext & { + [K in TName]: TReq extends true ? string : string | undefined; + } + > + >; } addNumberArgument<TName extends string, TReq extends boolean = false>(@@ -56,7 +67,13 @@ }
> > { this.args[name] = { ...options, type: "number" }; - return this; + return this as unknown as Command< + Prettify< + TContext & { + [K in TName]: TReq extends true ? number : number | undefined; + } + > + >; } addChoiceArgument<@@ -81,7 +98,15 @@ }
> > { this.args[name] = { ...options, type: "choice" }; - return this; + return this as unknown as Command< + Prettify< + TContext & { + [K in TName]: TReq extends true + ? TChoices[number] + : TChoices[number] | undefined; + } + > + >; } addPositional<TName extends string, TReq extends boolean = false>(@@ -96,13 +121,21 @@ >
> { this.positionals = this.positionals || []; this.positionals.push({ name, options }); - return this; + return this as unknown as Command< + Prettify< + TContext & { + [K in TName]: TReq extends true ? string : string | undefined; + } + > + >; } - addSubcommand(command: Command<any>): this { + addSubcommand<T extends Record<string, unknown>>(command: Command<T>): this { if (!command.name) throw new Error("Subcommands must have a name"); - this.subcommands[command.name] = command; + this.subcommands[command.name] = command as unknown as Command< + Record<string, unknown> + >; return this; }@@ -112,7 +145,7 @@ return this;
} async parse(argv: string[]): Promise<void> { - const ctx: any = {}; + const ctx: Record<string, unknown> = {}; const positionalsProvided: string[] = []; // init boolean flags to their defaults, or false.@@ -204,13 +237,13 @@ if (opts.required && ctx[name] === undefined)
throw new Error(`Missing required argument: --${name}`); } - if (this.actionFn) await this.actionFn(ctx); + if (this.actionFn) await this.actionFn(ctx as unknown as TContext); } private parseValue( name: string, value: string | undefined, - opts: ArgumentOptions<any>, + opts: ArgumentOptions<unknown>, ) { if (value === undefined) throw new Error(`Option --${name} requires a value`);@@ -223,7 +256,7 @@
return num; } if (opts.type === "choice") { - const choices = (opts as any).choices; + const choices = (opts as unknown as { choices: string[] }).choices; if (!choices.includes(value)) throw new Error( `Option --${name} must be one of: ${choices.join(", ")}`,@@ -232,3 +265,5 @@ }
return value; } } + +export * from "./types";