Skip to content

feat(cli/unstable): add generic for promptSelect() and promptMultipleSelect() #6713

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions cli/unstable_prompt_multiple_select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const SHOW_CURSOR = encoder.encode("\x1b[?25h");
/**
* Shows the given message and waits for the user's input. Returns the user's selected value as string.
*
* @typeParam T The type of the array elements.
*
* @param message The prompt message to show to the user.
* @param values The values for the prompt.
* @param options The options for the prompt.
Expand All @@ -40,11 +42,11 @@ const SHOW_CURSOR = encoder.encode("\x1b[?25h");
* const browsers = promptMultipleSelect("Please select browsers:", ["safari", "chrome", "firefox"], { clear: true });
* ```
*/
export function promptMultipleSelect(
export function promptMultipleSelect<T extends string>(
message: string,
values: string[],
values: readonly T[],
options: PromptMultipleSelectOptions = {},
): string[] | null {
): T[] | null {
if (!input.isTerminal()) return null;

const { clear } = options;
Expand Down Expand Up @@ -103,5 +105,5 @@ export function promptMultipleSelect(
output.writeSync(SHOW_CURSOR);
input.setRaw(false);

return [...selectedIndexes].map((it) => values[it] as string);
return [...selectedIndexes].map((it) => values[it] as string) as T[];
}
30 changes: 30 additions & 0 deletions cli/unstable_prompt_multiple_select_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -644,3 +644,33 @@ Deno.test("promptMultipleSelect() returns null if Deno.stdin.isTerminal() is fal
assertEquals(expectedOutput, actualOutput);
restore();
});

Deno.test("promptMultipleSelect() check return types", () => {
stub(Deno.stdin, "setRaw");
stub(Deno.stdin, "isTerminal", () => false);

promptMultipleSelect("Please select a browser:", [
"safari",
"chrome",
"firefox",
]) satisfies ("safari" | "chrome" | "firefox")[] | null;

const browsers = ["safari", "chrome", "firefox"] as const;
promptMultipleSelect("Please select a browsers:", browsers) satisfies
| ("safari" | "chrome" | "firefox")[]
| null;

promptMultipleSelect("Please select a browsers:", [
...browsers,
"edge",
]) satisfies
| ("safari" | "chrome" | "firefox" | "edge")[]
| null;

const selectItems = ["safari", "chrome", "firefox"];
promptMultipleSelect("Please select a browsers:", selectItems) satisfies
| string[]
| null;

restore();
});
8 changes: 5 additions & 3 deletions cli/unstable_prompt_select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const SHOW_CURSOR = encoder.encode("\x1b[?25h");
/**
* Shows the given message and waits for the user's input. Returns the user's selected value as string.
*
* @typeParam T The type of the array elements.
*
* @param message The prompt message to show to the user.
* @param values The values for the prompt.
* @param options The options for the prompt.
Expand All @@ -48,11 +50,11 @@ const SHOW_CURSOR = encoder.encode("\x1b[?25h");
* ], { clear: true, visibleLines: 3, indicator: "*" });
* ```
*/
export function promptSelect(
export function promptSelect<T extends string>(
message: string,
values: string[],
values: readonly T[],
options: PromptSelectOptions = {},
): string | null {
): T | null {
if (!input.isTerminal()) return null;

const SAFE_PADDING = 3;
Expand Down
30 changes: 30 additions & 0 deletions cli/unstable_prompt_select_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -816,3 +816,33 @@ Deno.test("promptSelect() handles ETX", () => {
assertEquals(expectedOutput, actualOutput);
restore();
});

Deno.test("promptSelect() check return types", () => {
stub(Deno.stdin, "setRaw");
stub(Deno.stdin, "isTerminal", () => false);

promptSelect("Please select a browser:", [
"safari",
"chrome",
"firefox",
]) satisfies "safari" | "chrome" | "firefox" | null;

const browsers = ["safari", "chrome", "firefox"] as const;
promptSelect("Please select a browser:", browsers) satisfies
| "safari"
| "chrome"
| "firefox"
| null;

promptSelect("Please select a browser:", [...browsers, "edge"]) satisfies
| "safari"
| "chrome"
| "firefox"
| "edge"
| null;

const selectItems = ["safari", "chrome", "firefox"];
promptSelect("Please select a browser:", selectItems) satisfies string | null;

restore();
});
Loading