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

Fix(inquirer): Allow string choices (backward compat with v9 & prior) #1530

Merged
merged 7 commits into from
Sep 1, 2024

Conversation

SBoudrias
Copy link
Owner

@SBoudrias SBoudrias commented Aug 30, 2024

Ref #1527

Inquirer v9 & prior allowed choices to be array of strings. Given how widespread that pattern is, we should maintain this backward compatibility. It'll also simplify the work of folks migrating from inquirer to @inquirer/prompts with less code change.

TODO

  • Select prompt
  • Checkbox prompt
  • Search prompt
  • Rawlist prompt
  • Documentation

@mshima
Copy link
Contributor

mshima commented Aug 30, 2024

I saw later that there is backward compatibility in place:

question.choices = choices.map(
(choice: string | number | { value?: string; name: string }) => {
if (typeof choice === 'string' || typeof choice === 'number') {
return { name: choice, value: choice };
} else if (!('value' in choice)) {
return { ...choice, value: choice.name };
}
return choice;
},
);
.

It's possible to add to backward compatibility types only:

type AddStringToArrayType<T extends any[]> = (T[number] | string)[];
type ArrayChoicesValueOrAsyncGetterFunction<T, A extends Answers> =
T extends Record<'choices', any>
?
| AddStringToArrayType<T['choices']>
| AsyncGetterFunction<AddStringToArrayType<T['choices']>, A>
: never;

Of course if the string support is added to choices, the backward compatibility can be dropped.

@SBoudrias
Copy link
Owner Author

Ohh I forgot about that. I'll still bring it over to @inquirer/* since it makes migration easier.

@SBoudrias SBoudrias marked this pull request as ready for review September 1, 2024 20:13
@SBoudrias SBoudrias merged commit 5f695f3 into main Sep 1, 2024
16 of 17 checks passed
@SBoudrias SBoudrias deleted the fix/string-options branch September 1, 2024 20:16
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