Skip to content

Commit f5413db

Browse files
authored
Types for version getter (#1982)
* Update types since .version() returns version. * Remove extra space
1 parent 4c095d1 commit f5413db

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

lib/command.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -1825,17 +1825,16 @@ Expecting one of '${allowedValues.join("', '")}'`);
18251825
}
18261826

18271827
/**
1828-
* Set the program version to `str`.
1828+
* Get or set the program version.
18291829
*
1830-
* This method auto-registers the "-V, --version" flag
1831-
* which will print the version number when passed.
1830+
* This method auto-registers the "-V, --version" option which will print the version number.
18321831
*
1833-
* You can optionally supply the flags and description to override the defaults.
1832+
* You can optionally supply the flags and description to override the defaults.
18341833
*
1835-
* @param {string} str
1834+
* @param {string} [str]
18361835
* @param {string} [flags]
18371836
* @param {string} [description]
1838-
* @return {this | string} `this` command for chaining, or version string if no arguments
1837+
* @return {this | string | undefined} `this` command for chaining, or version string if no arguments
18391838
*/
18401839

18411840
version(str, flags, description) {
@@ -1844,7 +1843,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
18441843
flags = flags || '-V, --version';
18451844
description = description || 'output the version number';
18461845
const versionOption = this.createOption(flags, description);
1847-
this._versionOptionName = versionOption.attributeName();
1846+
this._versionOptionName = versionOption.attributeName(); // [sic] not defined in constructor, partly legacy, partly only needed at root
18481847
this.options.push(versionOption);
18491848
this.on('option:' + versionOption.name(), () => {
18501849
this._outputConfiguration.writeOut(`${str}\n`);

typings/index.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,10 @@ export class Command {
294294
* You can optionally supply the flags and description to override the defaults.
295295
*/
296296
version(str: string, flags?: string, description?: string): this;
297+
/**
298+
* Get the program version.
299+
*/
300+
version(): string | undefined;
297301

298302
/**
299303
* Define a command, implemented using an action handler.

typings/index.test-d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ expectType<commander.Command | null>(program.parent);
3636
expectType<commander.Command>(program.version('1.2.3'));
3737
expectType<commander.Command>(program.version('1.2.3', '-r,--revision'));
3838
expectType<commander.Command>(program.version('1.2.3', '-r,--revision', 'show revision information'));
39+
expectType<string | undefined>(program.version());
3940

4041
// command (and CommandOptions)
4142
expectType<commander.Command>(program.command('action'));

0 commit comments

Comments
 (0)