diff --git a/cli/unstable_prompt_multiple_select.ts b/cli/unstable_prompt_multiple_select.ts index 76dc3607d898..1587045b88c1 100644 --- a/cli/unstable_prompt_multiple_select.ts +++ b/cli/unstable_prompt_multiple_select.ts @@ -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. @@ -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( message: string, - values: string[], + values: readonly T[], options: PromptMultipleSelectOptions = {}, -): string[] | null { +): T[] | null { if (!input.isTerminal()) return null; const { clear } = options; @@ -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[]; } diff --git a/cli/unstable_prompt_multiple_select_test.ts b/cli/unstable_prompt_multiple_select_test.ts index 1e0320aefac3..f7585f986955 100644 --- a/cli/unstable_prompt_multiple_select_test.ts +++ b/cli/unstable_prompt_multiple_select_test.ts @@ -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(); +}); diff --git a/cli/unstable_prompt_select.ts b/cli/unstable_prompt_select.ts index 7644c171c88f..e375defded86 100644 --- a/cli/unstable_prompt_select.ts +++ b/cli/unstable_prompt_select.ts @@ -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. @@ -48,11 +50,11 @@ const SHOW_CURSOR = encoder.encode("\x1b[?25h"); * ], { clear: true, visibleLines: 3, indicator: "*" }); * ``` */ -export function promptSelect( +export function promptSelect( message: string, - values: string[], + values: readonly T[], options: PromptSelectOptions = {}, -): string | null { +): T | null { if (!input.isTerminal()) return null; const SAFE_PADDING = 3; diff --git a/cli/unstable_prompt_select_test.ts b/cli/unstable_prompt_select_test.ts index eab084f7dd3e..471ec81e29f4 100644 --- a/cli/unstable_prompt_select_test.ts +++ b/cli/unstable_prompt_select_test.ts @@ -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(); +});