@commander-js/extra-typings
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -17,2 +17,10 @@ | ||
// This is a trick to encourage editor to suggest the known literals while still | ||
// allowing any BaseType value. | ||
// References: | ||
// - https://github.com/microsoft/TypeScript/issues/29729 | ||
// - https://github.com/sindresorhus/type-fest/blob/main/source/literal-union.d.ts | ||
// - https://github.com/sindresorhus/type-fest/blob/main/source/primitive.d.ts | ||
type LiteralUnion<LiteralType, BaseType extends string | number> = LiteralType | (BaseType & Record<never, never>); | ||
// Side note: not trying to represent arrays as non-empty, keep it simple. | ||
@@ -76,6 +84,6 @@ // https://stackoverflow.com/a/56006703/1082434 | ||
type CombineOptions<Options extends Record<string, any>, B extends Record<string, any>> = | ||
keyof B extends keyof Options | ||
? { [K in keyof Options]: K extends keyof B ? Options[K] | B[keyof B] : Options[K] } | ||
: Resolve<Options & B>; | ||
type CombineOptions<Options, O> = | ||
keyof O extends keyof Options | ||
? { [K in keyof Options]: K extends keyof O ? Options[K] | O[keyof O] : Options[K] } | ||
: Options & O; | ||
@@ -153,2 +161,4 @@ type IsAlwaysDefined<DefaulT, Mandatory extends boolean> = | ||
export type CommandUnknownOpts = Command<unknown[], OptionValues>; | ||
// ----- full copy of normal commander typings from here down, with extra type inference ----- | ||
@@ -299,3 +309,5 @@ | ||
* Set environment variable to check for option value. | ||
* Priority order of option values is default < env < cli | ||
* | ||
* An environment variables is only used if when processed the current option value is | ||
* undefined, or the source of the current value is 'default' or 'config' or 'env'. | ||
*/ | ||
@@ -357,5 +369,5 @@ env(name: string): this; | ||
/** Get the command term to show in the list of subcommands. */ | ||
subcommandTerm(cmd: Command): string; | ||
subcommandTerm(cmd: CommandUnknownOpts): string; | ||
/** Get the command summary to show in the list of subcommands. */ | ||
subcommandDescription(cmd: Command): string; | ||
subcommandDescription(cmd: CommandUnknownOpts): string; | ||
/** Get the option term to show in the list of options. */ | ||
@@ -371,21 +383,21 @@ optionTerm(option: Option): string; | ||
/** Get the command usage to be displayed at the top of the built-in help. */ | ||
commandUsage(cmd: Command): string; | ||
commandUsage(cmd: CommandUnknownOpts): string; | ||
/** Get the description for the command. */ | ||
commandDescription(cmd: Command): string; | ||
commandDescription(cmd: CommandUnknownOpts): string; | ||
/** Get an array of the visible subcommands. Includes a placeholder for the implicit help command, if there is one. */ | ||
visibleCommands(cmd: Command): Command[]; | ||
visibleCommands(cmd: CommandUnknownOpts): CommandUnknownOpts[]; | ||
/** Get an array of the visible options. Includes a placeholder for the implicit help option, if there is one. */ | ||
visibleOptions(cmd: Command): Option[]; | ||
visibleOptions(cmd: CommandUnknownOpts): Option[]; | ||
/** Get an array of the arguments which have descriptions. */ | ||
visibleArguments(cmd: Command): Argument[]; | ||
visibleArguments(cmd: CommandUnknownOpts): Argument[]; | ||
/** Get the longest command term length. */ | ||
longestSubcommandTermLength(cmd: Command, helper: Help): number; | ||
longestSubcommandTermLength(cmd: CommandUnknownOpts, helper: Help): number; | ||
/** Get the longest option term length. */ | ||
longestOptionTermLength(cmd: Command, helper: Help): number; | ||
longestOptionTermLength(cmd: CommandUnknownOpts, helper: Help): number; | ||
/** Get the longest argument term length. */ | ||
longestArgumentTermLength(cmd: Command, helper: Help): number; | ||
longestArgumentTermLength(cmd: CommandUnknownOpts, helper: Help): number; | ||
/** Calculate the pad width from the maximum term length. */ | ||
padWidth(cmd: Command, helper: Help): number; | ||
padWidth(cmd: CommandUnknownOpts, helper: Help): number; | ||
@@ -399,3 +411,3 @@ /** | ||
/** Generate the built-in help text. */ | ||
formatHelp(cmd: Command, helper: Help): string; | ||
formatHelp(cmd: CommandUnknownOpts, helper: Help): string; | ||
} | ||
@@ -425,13 +437,14 @@ export type HelpConfiguration = Partial<Help>; | ||
export type HookEvent = 'preSubcommand' | 'preAction' | 'postAction'; | ||
export type OptionValueSource = 'default' | 'env' | 'config' | 'cli'; | ||
// The source is a string so author can define their own too. | ||
export type OptionValueSource = LiteralUnion<'default' | 'config' | 'env' | 'cli' | 'implied', string> | undefined; | ||
export interface OptionValues { | ||
[key: string]: any; | ||
[key: string]: unknown; | ||
} | ||
export class Command<Args extends any[] = [], Opts = {}> { | ||
export class Command<Args extends any[] = [], Opts extends OptionValues = {}> { | ||
args: string[]; | ||
processedArgs: any[]; | ||
commands: Command[]; | ||
parent: Command | null; | ||
commands: CommandUnknownOpts[]; | ||
parent: CommandUnknownOpts | null; | ||
@@ -506,3 +519,3 @@ constructor(name?: string); | ||
*/ | ||
addCommand(cmd: Command, opts?: CommandOptions): this; | ||
addCommand(cmd: CommandUnknownOpts, opts?: CommandOptions): this; | ||
@@ -580,3 +593,3 @@ /** | ||
*/ | ||
hook(event: HookEvent, listener: (thisCommand: Command, actionCommand: Command) => void | Promise<void>): this; | ||
hook(event: HookEvent, listener: (thisCommand: this, actionCommand: CommandUnknownOpts) => void | Promise<void>): this; | ||
@@ -632,3 +645,3 @@ /** | ||
*/ | ||
copyInheritedSettings(sourceCommand: Command): this; | ||
copyInheritedSettings(sourceCommand: CommandUnknownOpts): this; | ||
@@ -760,3 +773,4 @@ /** | ||
*/ | ||
getOptionValue(key: string): any; | ||
getOptionValue<K extends keyof Opts>(key: K): Opts[K]; | ||
getOptionValue(key: string): unknown; | ||
@@ -766,3 +780,4 @@ /** | ||
*/ | ||
setOptionValue(key: string, value: unknown): this; | ||
setOptionValue<K extends keyof Opts>(key: K, value: unknown): this; | ||
setOptionValue(key: string, value: unknown): this; | ||
@@ -772,3 +787,4 @@ /** | ||
*/ | ||
setOptionValueWithSource(key: string, value: unknown, source: OptionValueSource): this; | ||
setOptionValueWithSource<K extends keyof Opts>(key: K, value: unknown, source: OptionValueSource): this; | ||
setOptionValueWithSource(key: string, value: unknown, source: OptionValueSource): this; | ||
@@ -778,3 +794,4 @@ /** | ||
*/ | ||
getOptionValueSource(key: string): OptionValueSource; | ||
getOptionValueSource<K extends keyof Opts>(key: K): OptionValueSource | undefined; | ||
getOptionValueSource(key: string): OptionValueSource | undefined; | ||
@@ -781,0 +798,0 @@ /** |
{ | ||
"name": "@commander-js/extra-typings", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Infer strong typings for commander options and action handlers", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
43431
950