Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow readonly arrays to be used as choices #1679

Closed
wants to merge 1 commit into from
Closed

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.

None yet

2 participants