Skip to content

Commit 16640f1

Browse files
ybiquitoussindresorhus
andauthoredFeb 14, 2020
Replace minimist-options types with own ones (#135)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent 5975fe6 commit 16640f1

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed
 

‎index.d.ts

+19-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
import {PackageJson} from 'type-fest';
2-
import {Options as MinimistOptions} from 'minimist-options';
32

43
declare namespace meow {
5-
interface Options<Flags extends MinimistOptions> {
4+
type FlagType = 'string' | 'boolean' | 'number';
5+
6+
interface Flag<Type extends FlagType, Default> {
7+
readonly type?: Type;
8+
readonly alias?: string;
9+
readonly default?: Default;
10+
}
11+
12+
type StringFlag = Flag<'string', string>;
13+
type BooleanFlag = Flag<'boolean', boolean>;
14+
type NumberFlag = Flag<'number', number>;
15+
16+
type AnyFlags = {[key: string]: StringFlag | BooleanFlag | NumberFlag};
17+
18+
interface Options<Flags extends AnyFlags> {
619
/**
720
Define argument flags.
821
@@ -159,7 +172,7 @@ declare namespace meow {
159172
readonly hardRejection?: boolean;
160173
}
161174

162-
type TypedFlags<Flags extends MinimistOptions> = {
175+
type TypedFlags<Flags extends AnyFlags> = {
163176
[F in keyof Flags]: Flags[F] extends {type: 'number'}
164177
? number
165178
: Flags[F] extends {type: 'string'}
@@ -169,7 +182,7 @@ declare namespace meow {
169182
: unknown;
170183
};
171184

172-
interface Result<Flags extends MinimistOptions> {
185+
interface Result<Flags extends AnyFlags> {
173186
/**
174187
Non-flag arguments.
175188
*/
@@ -246,7 +259,7 @@ const cli = meow(`
246259
foo(cli.input[0], cli.flags);
247260
```
248261
*/
249-
declare function meow<Flags extends MinimistOptions>(helpMessage: string, options?: meow.Options<Flags>): meow.Result<Flags>;
250-
declare function meow<Flags extends MinimistOptions>(options?: meow.Options<Flags>): meow.Result<Flags>;
262+
declare function meow<Flags extends meow.AnyFlags>(helpMessage: string, options?: meow.Options<Flags>): meow.Result<Flags>;
263+
declare function meow<Flags extends meow.AnyFlags>(options?: meow.Options<Flags>): meow.Result<Flags>;
251264

252265
export = meow;

‎index.test-d.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ expectType<Result<never>>(meow({hardRejection: false}));
3333
const result = meow('Help text', {
3434
flags: {
3535
foo: {type: 'boolean', alias: 'f'},
36-
'foo-bar': {type: 'number'}
36+
'foo-bar': {type: 'number'},
37+
bar: {type: 'string', default: ''}
3738
}}
3839
);
3940

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

4445
expectType<boolean>(result.flags.foo);
46+
expectType<unknown>(result.flags.fooBar);
47+
expectType<string>(result.flags.bar);
4548
expectType<boolean>(result.unnormalizedFlags.foo);
4649
expectType<unknown>(result.unnormalizedFlags.f);
4750
expectType<number>(result.unnormalizedFlags['foo-bar']);
48-
expectType<unknown>(result.flags.fooBar);
51+
expectType<string>(result.unnormalizedFlags.bar);
4952

5053
result.showHelp();
5154
result.showHelp(1);

0 commit comments

Comments
 (0)
Please sign in to comment.