Skip to content

Commit

Permalink
feat: export prompt option types (resolves #301)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jun 8, 2024
1 parent df0e555 commit 4a2ccbc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
37 changes: 19 additions & 18 deletions src/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type SelectOption = {
hint?: string;
};

export type TextOptions = {
export type TextPromptOptions = {
/**
* Specifies the prompt type as text.
* @optional
Expand All @@ -33,7 +33,7 @@ export type TextOptions = {
initial?: string;
};

export type ConfirmOptions = {
export type ConfirmPromptOptions = {
/**
* Specifies the prompt type as confirm.
*/
Expand All @@ -46,7 +46,7 @@ export type ConfirmOptions = {
initial?: boolean;
};

export type SelectOptions = {
export type SelectPromptOptions = {
/**
* Specifies the prompt type as select.
*/
Expand Down Expand Up @@ -90,22 +90,23 @@ export type MultiSelectOptions = {
* Defines a combined type for all prompt options.
*/
export type PromptOptions =
| TextOptions
| ConfirmOptions
| SelectOptions
| TextPromptOptions
| ConfirmPromptOptions
| SelectPromptOptions
| MultiSelectOptions;

type inferPromptReturnType<T extends PromptOptions> = T extends TextOptions
? string
: T extends ConfirmOptions
? boolean
: T extends SelectOptions
? T["options"][number] extends SelectOption
? T["options"][number]["value"]
: T["options"][number]
: T extends MultiSelectOptions
? T["options"]
: unknown;
type inferPromptReturnType<T extends PromptOptions> =
T extends TextPromptOptions
? string
: T extends ConfirmPromptOptions
? boolean
: T extends SelectPromptOptions
? T["options"][number] extends SelectOption
? T["options"][number]["value"]
: T["options"][number]
: T extends MultiSelectOptions
? T["options"]
: unknown;

/**
* Asynchronously prompts the user for input based on specified options.
Expand All @@ -118,7 +119,7 @@ type inferPromptReturnType<T extends PromptOptions> = T extends TextOptions
export async function prompt<
_ = any,
__ = any,
T extends PromptOptions = TextOptions,
T extends PromptOptions = TextPromptOptions,
>(
message: string,
opts: PromptOptions = {},
Expand Down
7 changes: 7 additions & 0 deletions src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ export { Consola } from "./consola";
export type * from "./types";
export type { ConsolaInstance } from "./consola";
export type { LogLevel, LogType } from "./constants";
export type {
PromptOptions,
ConfirmPromptOptions,
MultiSelectOptions,
SelectPromptOptions,
TextPromptOptions,
} from "./prompt";

0 comments on commit 4a2ccbc

Please sign in to comment.