Skip to content

Commit

Permalink
chore: improve type safety of CLI args (#6438)
Browse files Browse the repository at this point in the history
  • Loading branch information
nflaig authored Feb 16, 2024
1 parent a02ea75 commit 0cee060
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/cli/src/cmds/beacon/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ type ENRArgs = {
nat?: boolean;
};

const enrOptions: Record<string, CliOptionDefinition> = {
const enrOptions: CliCommandOptions<ENRArgs> = {
"enr.ip": {
description: "Override ENR IP entry",
type: "string",
Expand Down
17 changes: 14 additions & 3 deletions packages/cli/src/util/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,26 @@ export interface CliExample {
description?: string;
}

export interface CliOptionDefinition extends Options {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export interface CliOptionDefinition<T = any> extends Options {
example?: Omit<CliExample, "title">;
// Ensure `type` property matches type of `T`
type: T extends string
? "string"
: T extends number
? "number"
: T extends boolean
? "boolean"
: T extends Array<unknown>
? "array"
: never;
}

export type CliCommandOptions<OwnArgs> = Required<{
[K in keyof OwnArgs]: undefined extends OwnArgs[K]
? CliOptionDefinition
? CliOptionDefinition<OwnArgs[K]>
: // If arg cannot be undefined it must specify a default value
CliOptionDefinition & Required<Pick<Options, "default">>;
CliOptionDefinition<OwnArgs[K]> & Required<Pick<Options, "default">>;
}>;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down

0 comments on commit 0cee060

Please sign in to comment.