diff --git a/types/yargs/index.d.ts b/types/yargs/index.d.ts index a4e9b6bce5..4710fb779c 100644 --- a/types/yargs/index.d.ts +++ b/types/yargs/index.d.ts @@ -327,6 +327,7 @@ declare namespace yargs { O extends { boolean: true } ? boolean : O extends { number: true } ? number : O extends { string: true } ? string : + O extends { count: true } ? number : O extends { normalize: true } ? string : O extends { choices: Array } ? C : O extends { default: infer D } ? D : diff --git a/types/yargs/yargs-tests.ts b/types/yargs/yargs-tests.ts index 0d2f1d5bcd..8e07580fdd 100644 --- a/types/yargs/yargs-tests.ts +++ b/types/yargs/yargs-tests.ts @@ -667,6 +667,9 @@ function Argv$scriptName() { .scriptName("my-script"); } +type Color = "red" | "blue" | "green"; +const colors: Color[] = ["red", "blue", "green"]; + function Argv$inferOptionTypes() { // $ExpectType { [x: string]: any; array: string[]; boolean: boolean; count: number; number: number; string: string; _: string[]; $0: string; } yargs @@ -677,15 +680,36 @@ function Argv$inferOptionTypes() { .option("string", { type: "string" }) .argv; - type Color = "red" | "blue" | "green"; - const colors: Color[] = ["red", "blue", "green"]; + // $ExpectType { [x: string]: any; a: number; b: boolean; c: string; _: string[]; $0: string; } + yargs + .option("a", { default: 42 }) + .option("b", { default: false }) + .option("c", { default: "tmp" }) + .argv; + + // $ExpectType { [x: string]: any; array: string[]; boolean: boolean; number: number; string: string; _: string[]; $0: string; } + yargs + .option("array", { array: true }) + .option("boolean", { boolean: true }) + .option("number", { number: true }) + .option("string", { string: true }) + .argv; + + // $ExpectType { [x: string]: any; choices: Color; coerce: Date; count: number; normalize: string; _: string[]; $0: string; } + yargs + .option("choices", { choices: colors }) + .option("coerce", { coerce: () => new Date() }) + .option("count", { count: true }) + .option("normalize", { normalize: true }) + .argv; const argv = yargs .array("array") .boolean("boolean") - .choices("color", colors) + .choices("choices", colors) + .coerce("coerce", Date.parse) .count("count") - .default("date", new Date()) + .default("default", new Date()) .normalize("normalize") .number("number") .string("string") @@ -696,11 +720,13 @@ function Argv$inferOptionTypes() { // $ExpectType boolean argv.boolean; // $ExpectType Color - argv.color; + argv.choices; + // $ExpectType number + argv.coerce; // $ExpectType number argv.count; // $ExpectType Date - argv.date; + argv.default; // $ExpectType string argv.normalize; // $ExpectType number @@ -709,6 +735,29 @@ function Argv$inferOptionTypes() { argv.string; } +function Argv$inferMultipleOptionTypes() { + // $ExpectType { [x: string]: any; a: string; b: boolean; c: number; d: number; e: number; _: string[]; $0: string; } + yargs + .option({ a: { type: "string" }, b: { type: "boolean" } }) + .number(["c", "d", "e"]) + .argv; + + // $ExpectType { [x: string]: any; a: number; b: string; c: boolean; _: string[]; $0: string; } + yargs + .default({ a: 42, b: "b", c: false }) + .argv; + + // $ExpectType { [x: string]: any; a: number; b: string; c: Date; _: string[]; $0: string; } + yargs + .coerce({ a: Date.parse, b: String.prototype.toLowerCase, c: (s: string) => new Date(s) }) + .argv; + + // $ExpectType { [x: string]: any; a: number; b: string; c: Color; _: string[]; $0: string; } + yargs + .choices({ a: [1, 2, 3], b: ["black", "white"], c: colors }) + .argv; +} + function Argv$fallbackToAnyForUnknownOptions() { // $ExpectType any yargs.argv.bogus; @@ -741,19 +790,3 @@ function Argv$inferOptionTypesForAliases() { .alias("num", ["n", "count"]) .argv; } - -function Argv$inferOptionTypesFromCoercions() { - // $ExpectType number - yargs - .option("date", { coerce: Date.parse }) - .argv - .date; - - // $ExpectType { [x: string]: any; date: number; factor: number; _: string[]; $0: string; } - yargs - .options({ - date: { coerce: Date.parse }, - factor: { coerce: parseFloat } - }) - .argv; -}