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

Replace minimist-options types with own ones #135

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
ybiquitous marked this conversation as resolved.
Show resolved Hide resolved
readonly default?: Default;
}

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

type BaseFlags = { [key: string]: StringFlag | BooleanFlag | NumberFlag };
ybiquitous marked this conversation as resolved.
Show resolved Hide resolved

interface Options<Flags extends BaseFlags> {
/**
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 BaseFlags> = {
[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 BaseFlags> {
/**
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.BaseFlags>(helpMessage: string, options?: meow.Options<Flags>): meow.Result<Flags>;
declare function meow<Flags extends meow.BaseFlags>(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