Skip to content

Preset option type discrepancy #1973

Description

@aweebit
import { Option } from 'commander';

const option = new Option('--add <numbers...>')
  .argParser((value, previous?: number) => {
    return Number(previous) + Number(value);
  })
  .preset(123);
const prop = 'presetArg'; // since currently not declared in typings, see #1972
option.parseArg?.(option[prop] as Parameters<Option['preset']>[0], undefined);

This results in:

error TS2345: Argument of type 'unknown' is not assignable to parameter of type 'string'.

The reason is that the Option.preset() parameter type is set to unknown,

preset(arg: unknown): this;

while the parseArg type is designed in such a way that only strings are expected in the first argument.

parseArg?: <T>(value: string, previous: T) => T;

This is problematic because custom processing (parseArg) is called for preset option values.

(The reason why the call in the library's source did not result in an error when running the type check that led to #1967 is that the .preset() parameter type is any rather than unknown in lib/option.js:

* @param {any} arg
* @return {Option}
*/
preset(arg) {

I suspect this discrepancy is due to an expectation that vanilla JSDoc supports any but not unknown, but actually, there seems to be only one way to declare parameters that accept any arguments in vanilla JSDoc, and that is @param {*}, so unknown could be used here just as well.)

But anyway. My suggestion is that we require that the argument to Option.preset() be a string. Or we change the parseArg signature so that arbitrary values are accepted.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions