Skip to content

Commit

Permalink
Allow default property of Flag types to accept arrays (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertSasak authored Jul 15, 2021
1 parent d66fddc commit ae73466
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
10 changes: 5 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ Callback function to determine if a flag is required during runtime.
*/
export type IsRequiredPredicate = (flags: Readonly<AnyFlags>, input: readonly string[]) => boolean;

export interface Flag<Type extends FlagType, Default> {
export interface Flag<Type extends FlagType, Default, IsMultiple = false> {
readonly type?: Type;
readonly alias?: string;
readonly default?: Default;
readonly isRequired?: boolean | IsRequiredPredicate;
readonly isMultiple?: boolean;
readonly isMultiple?: IsMultiple;
}

type StringFlag = Flag<'string', string>;
type BooleanFlag = Flag<'boolean', boolean>;
type NumberFlag = Flag<'number', number>;
type StringFlag = Flag<'string', string> | Flag<'string', string[], true>;
type BooleanFlag = Flag<'boolean', boolean> | Flag<'boolean', boolean[], true>;
type NumberFlag = Flag<'number', number> | Flag<'number', number[], true>;
type AnyFlag = StringFlag | BooleanFlag | NumberFlag;
type AnyFlags = Record<string, AnyFlag>;

Expand Down
29 changes: 27 additions & 2 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {expectAssignable, expectType} from 'tsd';
import {expectAssignable, expectError, expectType} from 'tsd';
import {PackageJson} from 'type-fest';
import meow, {Result} from './index.js';
import meow, {Result, AnyFlag} from './index.js';

const importMeta = import.meta;

Expand All @@ -24,6 +24,15 @@ expectAssignable<{flags: {foo: string | undefined}}>(
expectAssignable<{flags: {foo: boolean | undefined}}>(
meow({importMeta, flags: {foo: {type: 'boolean'}}})
);
expectAssignable<{flags: {foo: number[] | undefined}}>(
meow({importMeta, flags: {foo: {type: 'number', isMultiple: true}}})
);
expectAssignable<{flags: {foo: string[] | undefined}}>(
meow({importMeta, flags: {foo: {type: 'string', isMultiple: true}}})
);
expectAssignable<{flags: {foo: boolean[] | undefined}}>(
meow({importMeta, flags: {foo: {type: 'boolean', isMultiple: true}}})
);
expectType<Result<never>>(meow({importMeta, description: 'foo'}));
expectType<Result<never>>(meow({importMeta, description: false}));
expectType<Result<never>>(meow({importMeta, help: 'foo'}));
Expand Down Expand Up @@ -79,3 +88,19 @@ const options = {
} as const;

meow('', options);

expectAssignable<AnyFlag>({type: 'string', default: 'cat'});
expectAssignable<AnyFlag>({type: 'number', default: 42});
expectAssignable<AnyFlag>({type: 'boolean', default: true});

expectAssignable<AnyFlag>({type: 'string', default: undefined});
expectAssignable<AnyFlag>({type: 'number', default: undefined});
expectAssignable<AnyFlag>({type: 'boolean', default: undefined});

expectAssignable<AnyFlag>({type: 'string', isMultiple: true, default: ['cat']});
expectAssignable<AnyFlag>({type: 'number', isMultiple: true, default: [42]});
expectAssignable<AnyFlag>({type: 'boolean', isMultiple: true, default: [false]});

expectError<AnyFlag>({type: 'string', isMultiple: true, default: 'cat'});
expectError<AnyFlag>({type: 'number', isMultiple: true, default: 42});
expectError<AnyFlag>({type: 'boolean', isMultiple: true, default: false});

0 comments on commit ae73466

Please sign in to comment.