Skip to content

Allow readonly arrays to be used as choices#1679

Closed
Pzixel wants to merge 1 commit intotj:masterfrom
Pzixel:patch-2
Closed

Allow readonly arrays to be used as choices#1679
Pzixel wants to merge 1 commit intotj:masterfrom
Pzixel:patch-2

Conversation

@Pzixel
Copy link

@Pzixel Pzixel commented Jan 16, 2022

Consider following usage:

const SupportedDrinks = ['small', 'medium', 'large'] as const;
type Drink = typeof SupportedDrinks[number];

const program = new Command()
      .addOption(new Option('-d, --drink <size>', 'drink size').choices(SupportedDrinks))

This allows user to get a strongly typed enum and reuse it to automatically populate supported drinks.
This code doesn't compile now though since readonly array is not assignable to just string[].

Pull Request

Problem

Solution

ChangeLog

Consider following usage:

```ts
const SupportedDrinks = ['small', 'medium', 'large'] as const;
type Drink = typeof SupportedDrinks[number];

const program = new Command()
      .addOption(new Option('-d, --drink <size>', 'drink size').choices(SupportedDrinks))
```

This allows user to get a strongly typed enum and reuse it to automatically populate supported drinks. 
This code doesn't compile now though since readonly array is not assignable to just `string[]`.
@Pzixel
Copy link
Author

Pzixel commented Jan 16, 2022

Sorry I didn't see there is a template for PRs, I've just sent it via regular "edit-and-create-PR" flow. Should I add any tests/more motivation here?

@shadowspawn
Copy link
Collaborator

shadowspawn commented Jan 16, 2022

This will be supported in Commander 9. See #1667. That PR started out fairly small too, but we then made code changes to make the usage of the parameters more robustly readonly.

Two PRs in quick succession, so readonly is something people are hitting. 😄

[Edit: corrected PR link. Twice!]

@Pzixel Pzixel deleted the patch-2 branch January 16, 2022 20:05
@Pzixel
Copy link
Author

Pzixel commented Jan 16, 2022

1679 is this PR, you probably meant to mention something else... 😅

@shadowspawn
Copy link
Collaborator

Cough. Try this one: #1667

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants