Skip to content

Commit

Permalink
fix: more custom flag type overloads (#471)
Browse files Browse the repository at this point in the history
* fix: more custom flag type overloads

* chore: fix test
  • Loading branch information
mdonnalley committed Aug 16, 2022
1 parent 12e31ff commit ac4baf2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/interfaces/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,11 @@ export type OptionFlag<T> = FlagBase<T, string> & OptionFlagProps & {
}

export type Definition<T> = {
(options: { multiple: true } & Partial<OptionFlag<T[]>>): OptionFlag<T[]>;
(
options: { multiple: true } & ({ required: true } | { default: Default<T> }) &
Partial<OptionFlag<T>>,
): OptionFlag<T[]>;
(options: { multiple: true } & Partial<OptionFlag<T[]>>): OptionFlag<T[] | undefined>;
(
options: ({ required: true } | { default: Default<T> }) &
Partial<OptionFlag<T>>,
Expand Down
12 changes: 9 additions & 3 deletions src/parser/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ export function boolean<T = boolean>(
} as BooleanFlag<T>
}

export function integer(opts: Partial<OptionFlag<number>> & {min?: number; max?: number } & {multiple: true} & ({required: true} | { default: Default<number> })): OptionFlag<number[]>
export function integer(opts: Partial<OptionFlag<number>> & {min?: number; max?: number } & {multiple: true}): OptionFlag<number[] | undefined>
export function integer(opts: Partial<OptionFlag<number>> & {min?: number; max?: number } & ({required: true} | { default: Default<number> })): OptionFlag<number>
export function integer(opts?: Partial<OptionFlag<number>> & {min?: number; max?: number }): OptionFlag<number | undefined>
export function integer(opts: Partial<OptionFlag<number>> & {min?: number; max?: number } = {}): OptionFlag<number> | OptionFlag<number | undefined> {
export function integer(opts: Partial<OptionFlag<number>> & {min?: number; max?: number } = {}): OptionFlag<number> | OptionFlag<number[]> | OptionFlag<number | undefined> | OptionFlag<number[] | undefined> {
return build({
...opts,
parse: async input => {
Expand All @@ -53,18 +55,22 @@ export function integer(opts: Partial<OptionFlag<number>> & {min?: number; max?:
})()
}

export function directory(opts: Partial<OptionFlag<string>> & { exists?: boolean } & {multiple: true} & ({required: true} | { default: Default<string> })): OptionFlag<string[]>
export function directory(opts: Partial<OptionFlag<string>> & { exists?: boolean } & {multiple: true}): OptionFlag<string[] | undefined>
export function directory(opts: { exists?: boolean } & Partial<OptionFlag<string>> & ({required: true} | { default: Default<string> })): OptionFlag<string>
export function directory(opts?: { exists?: boolean } & Partial<OptionFlag<string>>): OptionFlag<string | undefined>
export function directory(opts: { exists?: boolean } & Partial<OptionFlag<string>> = {}): OptionFlag<string> | OptionFlag<string | undefined> {
export function directory(opts: { exists?: boolean } & Partial<OptionFlag<string>> = {}): OptionFlag<string> | OptionFlag<string[]> | OptionFlag<string | undefined> | OptionFlag<string[] | undefined> {
return build<string>({
...opts,
parse: async (input: string) => opts.exists ? dirExists(input) : input,
})()
}

export function file(opts: Partial<OptionFlag<string>> & { exists?: boolean } & {multiple: true} & ({required: true} | { default: Default<string> })): OptionFlag<string[]>
export function file(opts: Partial<OptionFlag<string>> & { exists?: boolean } & {multiple: true}): OptionFlag<string[] | undefined>
export function file(opts: { exists?: boolean } & Partial<OptionFlag<string>> & ({required: true} | { default: string })): OptionFlag<string>
export function file(opts?: { exists?: boolean } & Partial<OptionFlag<string>>): OptionFlag<string | undefined>
export function file(opts: { exists?: boolean } & Partial<OptionFlag<string>> = {}): OptionFlag<string> | OptionFlag<string | undefined> {
export function file(opts: { exists?: boolean } & Partial<OptionFlag<string>> = {}): OptionFlag<string> | OptionFlag<string[]> | OptionFlag<string | undefined> | OptionFlag<string[] | undefined> {
return build<string>({
...opts,
parse: async (input: string) => opts.exists ? fileExists(input) : input,
Expand Down
2 changes: 1 addition & 1 deletion test/parser/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ See more help with --help`)
const out = await parse(['--bar', 'a', '--bar=b', '--foo=c', '--baz=d'], {
flags: {
foo: flags.string(),
bar: flags.string({multiple: true}),
bar: flags.string({multiple: true, required: true}),
baz: flags.string({required: true}),
},
})
Expand Down

0 comments on commit ac4baf2

Please sign in to comment.