diff --git a/package-lock.json b/package-lock.json index c8c6bcc0e..ab571e7e1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7320,9 +7320,9 @@ } }, "typescript": { - "version": "3.9.7", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.9.7.tgz", - "integrity": "sha512-BLbiRkiBzAwsjut4x/dsibSTB6yWpwT5qWmC2OfuCg3GgVQCSgMs4vEctYPhsaGtd0AeuuHMkjZ2h2WG8MSzRw==", + "version": "4.1.0-dev.20200915", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.1.0-dev.20200915.tgz", + "integrity": "sha512-zpey0vRjTtubK9FnG9AHAiOeAXrxrYUUuPuUisEegnxM/WwiYKx5P9xaP1lx/sMExfzGlQJd0kKq9go0dNLBGw==", "dev": true }, "union-value": { diff --git a/package.json b/package.json index fa5469c87..fbe23150f 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "eslint-plugin-jest": "^23.18.0", "jest": "^26.1.0", "standard": "^14.3.4", - "typescript": "^3.9.7" + "typescript": "^4.1.0-dev.20200915" }, "typings": "typings/index.d.ts", "jest": { diff --git a/typings/index.d.ts b/typings/index.d.ts index e404ae88d..998ff803b 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -27,9 +27,21 @@ declare namespace commander { from: 'node' | 'electron' | 'user'; } - interface Command { - [key: string]: any; // options as properties + type Strip = S extends `${infer T} ${infer _}` ? T : S; + + type CommandString = S extends `${infer _}--${infer T}` ? Strip : never; + + type CommandType = + S extends `${infer _}[${infer _}]` ? T | boolean : + S extends `${infer _}<${infer _}>` ? T : + boolean; + type ParseCommand = + string extends S ? { [key: string]: any } : + CommandString extends never ? never : + { [K in CommandString]: CommandType }; + + interface Command { args: string[]; commands: Command[]; @@ -171,9 +183,9 @@ declare namespace commander { * * @returns `this` command for chaining */ - option(flags: string, description?: string, defaultValue?: string | boolean): this; - option(flags: string, description: string, regexp: RegExp, defaultValue?: string | boolean): this; - option(flags: string, description: string, fn: (value: string, previous: T) => T, defaultValue?: T): this; + option(flags: S, description?: string, defaultValue?: T): this & Partial>; + option(flags: S, description: string, regexp: RegExp, defaultValue?: T): this & Partial>; + option(flags: S, description: string, fn: (value: string, previous: T) => T, defaultValue?: T): this & Partial>; /** * Define a required option, which must have a value after parsing. This usually means @@ -181,9 +193,9 @@ declare namespace commander { * * The `flags` string should contain both the short and long flags, separated by comma, a pipe or space. */ - requiredOption(flags: string, description?: string, defaultValue?: string | boolean): this; - requiredOption(flags: string, description: string, regexp: RegExp, defaultValue?: string | boolean): this; - requiredOption(flags: string, description: string, fn: (value: string, previous: T) => T, defaultValue?: T): this; + requiredOption(flags: S, description?: string, defaultValue?: T): this & ParseCommand; + requiredOption(flags: S, description: string, regexp: RegExp, defaultValue?: T): this & ParseCommand; + requiredOption(flags: S, description: string, fn: (value: string, previous: T) => T, defaultValue?: T): this & ParseCommand; /** * Whether to store option values as properties on command object,