Skip to content

Commit

Permalink
feat(yargs): add types for yargs@v17 (#52169)
Browse files Browse the repository at this point in the history
* feat(yargs): update types to reflect yargs@v17

* test: address failures

* fail should accept boolean
  • Loading branch information
bcoe committed May 21, 2021
1 parent 8d4f9d4 commit 2772f88
Show file tree
Hide file tree
Showing 8 changed files with 2,364 additions and 164 deletions.
26 changes: 13 additions & 13 deletions types/yargs/index.d.ts
@@ -1,4 +1,4 @@
// Type definitions for yargs 16.0
// Type definitions for yargs 17.0
// Project: https://github.com/chevex/yargs, https://yargs.js.org
// Definitions by: Martin Poelstra <https://github.com/poelstra>
// Mizunashi Mana <https://github.com/mizunashi-mana>
Expand All @@ -8,6 +8,7 @@
// Emily Marigold Klassen <https://github.com/forivall>
// ExE Boss <https://github.com/ExE-Boss>
// Aankhen <https://github.com/Aankhen>
// Ben Coe <https://github.com/bcoe>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 3.0

Expand Down Expand Up @@ -44,7 +45,7 @@ declare namespace yargs {
* `Arguments<T>` to simplify the inferred type signature in client code.
*/
interface Argv<T = {}> {
(): { [key in keyof Arguments<T>]: Arguments<T>[key] };
(): { [key in keyof Arguments<T>]: Arguments<T>[key] } | Promise<{ [key in keyof Arguments<T>]: Arguments<T>[key] }>;
(args: ReadonlyArray<string>, cwd?: string): Argv<T>;

/**
Expand All @@ -70,7 +71,7 @@ declare namespace yargs {
* it will ignore the first parameter since it expects it to be the script name. In order to override
* this behavior, use `.parse(process.argv.slice(1))` instead of .argv and the first parameter won't be ignored.
*/
argv: { [key in keyof Arguments<T>]: Arguments<T>[key] };
argv: { [key in keyof Arguments<T>]: Arguments<T>[key] } | Promise<{ [key in keyof Arguments<T>]: Arguments<T>[key] }>;

/**
* Tell the parser to interpret `key` as an array.
Expand Down Expand Up @@ -332,7 +333,7 @@ declare namespace yargs {
* Method to execute when a failure occurs, rather than printing the failure message.
* @param func Is called with the failure message that would have been printed, the Error instance originally thrown and yargs state when the failure occurred.
*/
fail(func: (msg: string, err: Error, yargs: Argv<T>) => any): Argv<T>;
fail(func: ((msg: string, err: Error, yargs: Argv<T>) => any) | boolean): Argv<T>;

/**
* Allows to programmatically get completion choices for any line.
Expand Down Expand Up @@ -453,8 +454,13 @@ declare namespace yargs {
* Note: Providing a callback to parse() disables the `exitProcess` setting until after the callback is invoked.
* @param [context] Provides a useful mechanism for passing state information to commands
*/
parse(): { [key in keyof Arguments<T>]: Arguments<T>[key] };
parse(arg: string | ReadonlyArray<string>, context?: object, parseCallback?: ParseCallback<T>): { [key in keyof Arguments<T>]: Arguments<T>[key] };
parse(): { [key in keyof Arguments<T>]: Arguments<T>[key] } | Promise<{ [key in keyof Arguments<T>]: Arguments<T>[key] }>;
parse(arg: string | ReadonlyArray<string>, context?: object, parseCallback?: ParseCallback<T>): { [key in keyof Arguments<T>]: Arguments<T>[key] }
| Promise<{ [key in keyof Arguments<T>]: Arguments<T>[key] }>;
parseSync(): { [key in keyof Arguments<T>]: Arguments<T>[key] };
parseSync(arg: string | ReadonlyArray<string>, context?: object, parseCallback?: ParseCallback<T>): { [key in keyof Arguments<T>]: Arguments<T>[key] };
parseAsync(): Promise<{ [key in keyof Arguments<T>]: Arguments<T>[key] }>;
parseAsync(arg: string | ReadonlyArray<string>, context?: object, parseCallback?: ParseCallback<T>): Promise<{ [key in keyof Arguments<T>]: Arguments<T>[key] }>;

/**
* If the arguments have not been parsed, this property is `false`.
Expand Down Expand Up @@ -508,12 +514,6 @@ declare namespace yargs {

requiresArg(key: string | ReadonlyArray<string>): Argv<T>;

/**
* @deprecated since version 6.6.0
* Use '.global()' instead
*/
reset(): Argv<T>;

/** Set the name of your script ($0). Default is the base filename executed by node (`process.argv[1]`) */
scriptName($0: string): Argv<T>;

Expand Down Expand Up @@ -836,7 +836,7 @@ declare namespace yargs {
handler: (args: Arguments<U>) => void;
}

type ParseCallback<T = {}> = (err: Error | undefined, argv: Arguments<T>, output: string) => void;
type ParseCallback<T = {}> = (err: Error | undefined, argv: Arguments<T>|Promise<Arguments<T>>, output: string) => void;
type CommandBuilder<T = {}, U = {}> = { [key: string]: Options } | ((args: Argv<T>) => Argv<U>) | ((args: Argv<T>) => PromiseLike<Argv<U>>);
type SyncCompletionFunction = (current: string, argv: any) => string[];
type AsyncCompletionFunction = (current: string, argv: any, done: (completion: ReadonlyArray<string>) => void) => void;
Expand Down
5 changes: 5 additions & 0 deletions types/yargs/v16/helpers.d.ts
@@ -0,0 +1,5 @@
import * as Parser from 'yargs-parser';

export function applyExtends(config: Record<string, any>, cwd: string, mergeExtends: boolean): Record<string, any>;
export function hideBin(argv: string[]): string[];
export { Parser };

0 comments on commit 2772f88

Please sign in to comment.