Add string[] to options defaultValue type#1721
Conversation
|
The problem is that the My recollection is in theory the default value can be anything, but in practice it is usually a |
option and requiredOption's defaultValue parameter was `string |
boolean` but the default for a variadic option should be an array.
```
program.option('--var <args...>', 'variadic arguments', ['1'])
program.parse([])
> { var: ['1'] }
program.parse(['--var', '1', '2'])
> { var: ['1', '2'] }
```
If you use a string default you have to handle opts that are strings
```
// with a string arg the default value is a string.
program.option('--var <args...>', 'variadic arguments', '1')
program.parse([])
> { var: '1' }
program.parse(['--var', '1', '2'])
> { var: ['1', '2'] }
```
`unknown` matches the jsdoc comment and the typings for argument(name:
string, description?: string, defaultValue?: unknown): this` but
conflicts with other `option` overloads.
commander will pass thru any defaultValue to parse opts so `any` or
`unknown` are good choices, but `string | boolean | string[]` reflects
the values that commander will return when it parses arguments without a
coerce function.
5c4fc3d to
8ca3c3d
Compare
|
that makes sense, I didn't consider the overlaps between override functions. I updated the PR to add string[]. |
|
For historical interest, added variadic after the tighter typing was introduced:
|
|
It might be next week before I have time to look into the constraints, but I think |
|
Some extra information for future readers. The default value can technically be any type, but in practice is almost always:
If we use If needed, the author can specify some other type without using an explicit coercion by using |
|
Included in Commander v9.3.0. Thanks @everett1992 |
Pull Request
Problem
The default value for a variadic option should be an array but the type requires a boolean or string.
If you pass a string as the default value to a variadic option the default value will be a string and you'll have to handle string or string[] when using the options. If you pass an array you only have to handle arrays, and you can pass a multi-valued default.
Solution
I added string[] to option's defaultValue.
ChangeLog
Add string[] to options defaultValue type.