UNPKG

4.57 kBTypeScriptView Raw
1// Type definitions for prompts 2.0
2// Project: https://github.com/terkelg/prompts
3// Definitions by: Berkay GURSOY <https://github.com/Berkays>
4// Daniel Perez Alvarez <https://github.com/unindented>
5// Kamontat Chantrachirathumrong <https://github.com/kamontat>
6// theweirdone <https://github.com/theweirdone>
7// whoaa512 <https://github.com/whoaa512>
8// John Reilly <https://github.com/johnnyreilly>
9// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
10// TypeScript Version: 2.9
11
12/// <reference types="node" />
13
14export = prompts;
15
16import { Readable, Writable } from 'stream';
17
18declare function prompts<T extends string = string>(
19 questions: prompts.PromptObject<T> | Array<prompts.PromptObject<T>>,
20 options?: prompts.Options
21): Promise<prompts.Answers<T>>;
22
23declare namespace prompts {
24 // Circular reference from prompts
25 const prompt: any;
26
27 function inject(arr: ReadonlyArray<any>): void;
28
29 namespace inject {
30 const prototype: {};
31 }
32
33 function override(obj: { [key: string]: any }): void;
34
35 namespace override {
36 const prototype: {};
37 }
38
39 namespace prompts {
40 function autocomplete(args: PromptObject): any;
41
42 function confirm(args: PromptObject): void;
43
44 function date(args: PromptObject): any;
45
46 function invisible(args: PromptObject): any;
47
48 function list(args: PromptObject): any;
49
50 function multiselect(args: PromptObject): any;
51
52 function number(args: PromptObject): void;
53
54 function password(args: PromptObject): any;
55
56 function select(args: PromptObject): void;
57
58 function text(args: PromptObject): void;
59
60 function toggle(args: PromptObject): void;
61 }
62
63 // Based upon: https://github.com/terkelg/prompts/blob/d7d2c37a0009e3235b2e88a7d5cdbb114ac271b2/lib/elements/select.js#L29
64 interface Choice {
65 title: string;
66 value?: any;
67 disabled?: boolean | undefined;
68 selected?: boolean | undefined;
69 description?: string | undefined;
70 }
71
72 interface Options {
73 onSubmit?: ((prompt: PromptObject, answer: any, answers: any[]) => void) | undefined;
74 onCancel?: ((prompt: PromptObject, answers: any) => void) | undefined;
75 }
76
77 interface PromptObject<T extends string = string> {
78 type: PromptType | Falsy | PrevCaller<T, PromptType | Falsy>;
79 name: ValueOrFunc<T>;
80 message?: ValueOrFunc<string> | undefined;
81 initial?: InitialReturnValue | PrevCaller<T, InitialReturnValue | Promise<InitialReturnValue>> | undefined;
82 style?: string | PrevCaller<T, string | Falsy> | undefined;
83 format?: PrevCaller<T, void> | undefined;
84 validate?: PrevCaller<T, boolean | string | Promise<boolean | string>> | undefined;
85 onState?: PrevCaller<T, void> | undefined;
86 min?: number | PrevCaller<T, number | Falsy> | undefined;
87 max?: number | PrevCaller<T, number | Falsy> | undefined;
88 float?: boolean | PrevCaller<T, boolean | Falsy> | undefined;
89 round?: number | PrevCaller<T, number | Falsy> | undefined;
90 instructions?: string | boolean | undefined;
91 increment?: number | PrevCaller<T, number | Falsy> | undefined;
92 separator?: string | PrevCaller<T, string | Falsy> | undefined;
93 active?: string | PrevCaller<T, string | Falsy> | undefined;
94 inactive?: string | PrevCaller<T, string | Falsy> | undefined;
95 choices?: Choice[] | PrevCaller<T, Choice[] | Falsy> | undefined;
96 hint?: string | PrevCaller<T, string | Falsy> | undefined;
97 warn?: string | PrevCaller<T, string | Falsy> | undefined;
98 suggest?: ((input: any, choices: Choice[]) => Promise<any>) | undefined;
99 limit?: number | PrevCaller<T, number | Falsy> | undefined;
100 mask?: string | PrevCaller<T, string | Falsy> | undefined;
101 stdout?: Writable | undefined;
102 stdin?: Readable | undefined;
103 }
104
105 type Answers<T extends string> = { [id in T]: any };
106
107 type PrevCaller<T extends string, R = T> = (
108 prev: any,
109 values: Answers<T>,
110 prompt: PromptObject
111 ) => R;
112
113 type Falsy = false | null | undefined;
114
115 type PromptType = "text" | "password" | "invisible" | "number" | "confirm" | "list" | "toggle" | "select" | "multiselect" | "autocomplete" | "date" | "autocompleteMultiselect";
116
117 type ValueOrFunc<T extends string> = T | PrevCaller<T>;
118
119 type InitialReturnValue = string | number | boolean | Date;
120}