Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Replace minimist-options types with own ones (#135)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
ybiquitous and sindresorhus committed Feb 14, 2020
1 parent 5975fe6 commit 16640f1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
25 changes: 19 additions & 6 deletions index.d.ts
@@ -1,8 +1,21 @@
import {PackageJson} from 'type-fest';
import {Options as MinimistOptions} from 'minimist-options';

declare namespace meow {
interface Options<Flags extends MinimistOptions> {
type FlagType = 'string' | 'boolean' | 'number';

interface Flag<Type extends FlagType, Default> {
readonly type?: Type;
readonly alias?: string;
readonly default?: Default;
}

type StringFlag = Flag<'string', string>;
type BooleanFlag = Flag<'boolean', boolean>;
type NumberFlag = Flag<'number', number>;

type AnyFlags = {[key: string]: StringFlag | BooleanFlag | NumberFlag};

interface Options<Flags extends AnyFlags> {
/**
Define argument flags.
Expand Down Expand Up @@ -159,7 +172,7 @@ declare namespace meow {
readonly hardRejection?: boolean;
}

type TypedFlags<Flags extends MinimistOptions> = {
type TypedFlags<Flags extends AnyFlags> = {
[F in keyof Flags]: Flags[F] extends {type: 'number'}
? number
: Flags[F] extends {type: 'string'}
Expand All @@ -169,7 +182,7 @@ declare namespace meow {
: unknown;
};

interface Result<Flags extends MinimistOptions> {
interface Result<Flags extends AnyFlags> {
/**
Non-flag arguments.
*/
Expand Down Expand Up @@ -246,7 +259,7 @@ const cli = meow(`
foo(cli.input[0], cli.flags);
```
*/
declare function meow<Flags extends MinimistOptions>(helpMessage: string, options?: meow.Options<Flags>): meow.Result<Flags>;
declare function meow<Flags extends MinimistOptions>(options?: meow.Options<Flags>): meow.Result<Flags>;
declare function meow<Flags extends meow.AnyFlags>(helpMessage: string, options?: meow.Options<Flags>): meow.Result<Flags>;
declare function meow<Flags extends meow.AnyFlags>(options?: meow.Options<Flags>): meow.Result<Flags>;

export = meow;
7 changes: 5 additions & 2 deletions index.test-d.ts
Expand Up @@ -33,7 +33,8 @@ expectType<Result<never>>(meow({hardRejection: false}));
const result = meow('Help text', {
flags: {
foo: {type: 'boolean', alias: 'f'},
'foo-bar': {type: 'number'}
'foo-bar': {type: 'number'},
bar: {type: 'string', default: ''}
}}
);

Expand All @@ -42,10 +43,12 @@ expectType<PackageJson>(result.pkg);
expectType<string>(result.help);

expectType<boolean>(result.flags.foo);
expectType<unknown>(result.flags.fooBar);
expectType<string>(result.flags.bar);
expectType<boolean>(result.unnormalizedFlags.foo);
expectType<unknown>(result.unnormalizedFlags.f);
expectType<number>(result.unnormalizedFlags['foo-bar']);
expectType<unknown>(result.flags.fooBar);
expectType<string>(result.unnormalizedFlags.bar);

result.showHelp();
result.showHelp(1);
Expand Down

0 comments on commit 16640f1

Please sign in to comment.