Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improve type safety of CLI args #6438

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading