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

Use const type parameters so author does not need to add "as const" to get narrow types for choices #79

Merged
merged 1 commit into from
Nov 2, 2024

Conversation

shadowspawn
Copy link
Contributor

Pull Request

This is inspired by #61 and goes a bit further.

Reminder to self: have @matthyk as co-author.

Problem

To get narrow types for options and arguments with choices, author needs to add as const to encourage TypeScript to narrow the type.

Solution

TypeScript 5.0 adds const type parameters to use narrow types by default. Use this in particular for .choices(), but also for .default() and .preset() for consistency when used together.

ChangeLog

  • infer narrow types for choices, so no longer need to specify as const
  • Typescript 5.0 or higher is required

Co-authored-by: matthyk <53833818+matthyk@users.noreply.github.com>
@shadowspawn shadowspawn added pending release On a branch for eventual release, but not yet merged to main. semver:major labels Nov 2, 2024
@shadowspawn shadowspawn merged commit c274405 into release/13.x Nov 2, 2024
18 checks passed
@shadowspawn shadowspawn deleted the feature/narrow-choices branch November 2, 2024 03:02
@shadowspawn
Copy link
Contributor Author

Thanks @abetomo

Very quick!

@shadowspawn shadowspawn mentioned this pull request Nov 6, 2024
@shadowspawn
Copy link
Contributor Author

A prerelease is available for v13. The release is tagged as next and can be installed with:

npm install @commander-js/extra-typings@next

@shadowspawn
Copy link
Contributor Author

Released in v13.0.0

@shadowspawn shadowspawn removed the pending release On a branch for eventual release, but not yet merged to main. label Dec 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants