-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
types.d.ts
42 lines (31 loc) · 1.54 KB
/
types.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
interface BasePrompt {
name: string;
}
interface TextPrompt extends BasePrompt {
type: 'text';
}
interface ConfirmPrompt extends BasePrompt {
type: 'confirm';
}
interface SelectPrompt<Choices extends Readonly<Readonly<Choice>[]>> extends BasePrompt {
type: 'select';
choices: Choices;
}
interface MultiselectPrompt<Choices extends Readonly<Readonly<Choice>[]>> extends BasePrompt {
type: 'multiselect';
choices: Choices;
}
interface Choice {
value: any;
label: string;
hint?: string;
}
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never
type Prompt = TextPrompt | ConfirmPrompt | SelectPrompt<Readonly<Choice[]>> | MultiselectPrompt<Readonly<Choice[]>>;
type Answer<T extends Prompt, Choices extends Readonly<Choice[]> = T extends SelectPrompt<infer C> ? C : T extends MultiselectPrompt<infer C> ? C : never> = T extends TextPrompt ? string : T extends ConfirmPrompt ? boolean : T extends SelectPrompt<Choices> ? Choices[number]['value'] : T extends MultiselectPrompt<Choices> ? (Choices[number]['value'] | undefined)[] : never;
type Answers<T extends Readonly<Prompt> | Readonly<Prompt[]>> = T extends Readonly<Prompt> ? Partial<{ [key in T['name']]: Answer<T> }> : T extends Readonly<Prompt[]> ? UnionToIntersection<Answers<T[number]>> : never;
interface PromptOptions {
onSubmit?: () => any;
onCancel?: () => any;
}
export default function prompt<T extends Readonly<Prompt> | Readonly<Prompt[]>>(questions: T, opts?: PromptOptions): Promise<Answers<T>>;