UNPKG

24.6 kBTypeScriptView Raw
1// Type definitions for inquirer 7.3
2// Project: https://github.com/SBoudrias/Inquirer.js
3// Definitions by: Qubo <https://github.com/tkQubo>
4// Parvez <https://github.com/ppathan>
5// Jouderian <https://github.com/jouderianjr>
6// Qibang <https://github.com/bang88>
7// Jason Dreyzehner <https://github.com/bitjson>
8// Synarque <https://github.com/synarque>
9// Justin Rockwood <https://github.com/jrockwood>
10// Keith Kelly <https://github.com/kwkelly>
11// Richard Lea <https://github.com/chigix>
12// Jed Mao <https://github.com/jedmao>
13// Manuel Thalmann <https://github.com/manuth>
14// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
15// TypeScript Version: 3.3
16import { Interface as ReadlineInterface } from "readline";
17import { Observable } from "rxjs";
18import Choice = require("./lib/objects/choice");
19import Choices = require("./lib/objects/choices");
20import Separator = require("./lib/objects/separator");
21import "./lib/prompts/base";
22import "./lib/prompts/checkbox";
23import "./lib/prompts/confirm";
24import "./lib/prompts/editor";
25import "./lib/prompts/expand";
26import "./lib/prompts/input";
27import "./lib/prompts/list";
28import "./lib/prompts/number";
29import "./lib/prompts/password";
30import "./lib/prompts/rawlist";
31import "./lib/utils/events";
32import "./lib/utils/paginator";
33import "./lib/utils/readline";
34import "./lib/utils/screen-manager";
35import "./lib/utils/utils";
36import BottomBar = require("./lib/ui/bottom-bar");
37import PromptUI = require("./lib/ui/prompt");
38
39/**
40 * Represents a union which preserves autocompletion.
41 *
42 * @template T
43 * The keys which are available for autocompletion.
44 *
45 * @template F
46 * The fallback-type.
47 */
48type LiteralUnion<T extends F, F = string> = T | (F & {});
49
50/**
51 * Provides prompts for answering questions.
52 */
53interface PromptModuleBase {
54 /**
55 * Registers a new prompt-type.
56 *
57 * @param name
58 * The name of the prompt.
59 *
60 * @param prompt
61 * The constructor of the prompt.
62 */
63 registerPrompt(name: string, prompt: inquirer.prompts.PromptConstructor): void;
64
65 /**
66 * Registers the default prompts.
67 */
68 restoreDefaultPrompts(): void;
69}
70
71/**
72 * Represents a list-based question.
73 *
74 * @template T
75 * The type of the answers.
76 *
77 * @template TChoiceMap
78 * The valid choices for the question.
79 */
80interface ListQuestionOptionsBase<T, TChoiceMap> extends inquirer.Question<T> {
81 /**
82 * The choices of the prompt.
83 */
84 choices?: inquirer.AsyncDynamicQuestionProperty<ReadonlyArray<inquirer.DistinctChoice<TChoiceMap>>, T>;
85
86 /**
87 * The number of elements to show on each page.
88 */
89 pageSize?: number;
90}
91
92/**
93 * Provides components for the module.
94 */
95declare namespace inquirer {
96 /**
97 * Represents either a key of `T` or a `string`.
98 *
99 * @template T
100 * The type of the keys to suggest.
101 */
102 type KeyUnion<T> = LiteralUnion<Extract<keyof T, string>>;
103
104 /**
105 * Converts the specified union-type `U` to an intersection-type.
106 *
107 * @template U
108 * The union to convert to an intersection.
109 */
110 type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
111
112 /**
113 * Provides an input and an output-stream.
114 */
115 interface StreamOptions {
116 /**
117 * A stream to read the input from.
118 */
119 input?: NodeJS.ReadStream;
120
121 /**
122 * A stream to write the output to.
123 */
124 output?: NodeJS.WriteStream;
125 }
126
127 /**
128 * Provides the functionality to prompt questions to the user.
129 */
130 interface PromptModule extends PromptModuleBase {
131 /**
132 * The prompts of the prompt-module.
133 */
134 prompts: prompts.PromptCollection;
135
136 /**
137 * Prompts the questions to the user.
138 */
139 <T>(questions: QuestionCollection<T>, initialAnswers?: Partial<T>): Promise<T> & { ui: PromptUI };
140
141 /**
142 * Registers a new prompt-type.
143 *
144 * @param name
145 * The name of the prompt.
146 *
147 * @param prompt
148 * The constructor of the prompt.
149 */
150 registerPrompt(name: string, prompt: prompts.PromptConstructor): this;
151 }
152
153 interface Inquirer extends PromptModuleBase {
154 /**
155 * Registers a new prompt-type.
156 *
157 * @param name
158 * The name of the prompt.
159 *
160 * @param prompt
161 * The constructor of the prompt.
162 */
163 registerPrompt(name: string, prompt: prompts.PromptConstructor): void;
164
165 /**
166 * Creates a prompt-module.
167 *
168 * @param opt
169 * The streams for the prompt-module.
170 *
171 * @returns
172 * The new prompt-module.
173 */
174 createPromptModule(opt?: StreamOptions): PromptModule;
175
176 /**
177 * The default prompt-module.
178 */
179 prompt: PromptModule;
180
181 /**
182 * The prompts of the default prompt-module.
183 *
184 * @deprecated
185 */
186 prompts: {};
187
188 /**
189 * Represents a choice-item separator.
190 */
191 Separator: typeof Separator;
192
193 /**
194 * Provides ui-components.
195 */
196 ui: {
197 /**
198 * Represents the bottom-bar UI.
199 */
200 BottomBar: typeof BottomBar;
201
202 /**
203 * Represents the prompt ui.
204 */
205 Prompt: typeof PromptUI;
206 };
207 }
208
209 /**
210 * A set of answers.
211 */
212 interface Answers extends Record<string, any> { }
213
214 /**
215 * Provides the functionality to validate answers.
216 *
217 * @template T
218 * The type of the answers.
219 */
220 type Validator<T extends Answers = Answers> = Question<T>["validate"];
221
222 /**
223 * Provides the functionality to transform an answer.
224 *
225 * @template T
226 * The type of the answers.
227 */
228 type Transformer<T extends Answers = Answers> = InputQuestionOptions<T>["transformer"];
229
230 /**
231 * Represents a dynamic property for a question.
232 */
233 type DynamicQuestionProperty<T, TAnswers extends Answers = Answers> = T | ((answers: TAnswers) => T);
234
235 /**
236 * Represents a dynamic property for a question which can be fetched asynchronously.
237 */
238 type AsyncDynamicQuestionProperty<T, TAnswers extends Answers = Answers> = DynamicQuestionProperty<T | Promise<T>, TAnswers>;
239
240 /**
241 * Provides options for a question.
242 *
243 * @template T
244 * The type of the answers.
245 */
246 interface Question<T extends Answers = Answers> {
247 /**
248 * The type of the question.
249 */
250 type?: string;
251
252 /**
253 * The key to save the answer to the answers-hash.
254 */
255 name?: KeyUnion<T>;
256
257 /**
258 * The message to show to the user.
259 */
260 message?: AsyncDynamicQuestionProperty<string, T>;
261
262 /**
263 * The default value of the question.
264 */
265 default?: AsyncDynamicQuestionProperty<any, T>;
266
267 /**
268 * The prefix of the `message`.
269 */
270 prefix?: string;
271
272 /**
273 * The suffix of the `message`.
274 */
275 suffix?: string;
276
277 /**
278 * Post-processes the answer.
279 *
280 * @param input
281 * The answer provided by the user.
282 *
283 * @param answers
284 * The answers provided by the user.
285 */
286 filter?(input: any, answers: T): any;
287
288 /**
289 * A value indicating whether the question should be prompted.
290 */
291 when?: AsyncDynamicQuestionProperty<boolean, T>;
292
293 /**
294 * Validates the integrity of the answer.
295 *
296 * @param input
297 * The answer provided by the user.
298 *
299 * @param answers
300 * The answers provided by the user.
301 *
302 * @returns
303 * Either a value indicating whether the answer is valid or a `string` which describes the error.
304 */
305 validate?(input: any, answers?: T): boolean | string | Promise<boolean | string>;
306 }
307
308 /**
309 * Represents a choice-item.
310 */
311 interface ChoiceBase {
312 /**
313 * The type of the choice.
314 */
315 type?: string;
316 }
317
318 /**
319 * Provides options for a choice.
320 *
321 * @template T
322 * The type of the answers.
323 */
324 interface ChoiceOptions<T extends Answers = Answers> extends ChoiceBase {
325 /**
326 * @inheritdoc
327 */
328 type?: "choice";
329
330 /**
331 * The name of the choice to show to the user.
332 */
333 name?: string;
334
335 /**
336 * The value of the choice.
337 */
338 value?: any;
339
340 /**
341 * The short form of the name of the choice.
342 */
343 short?: string;
344
345 /**
346 * The extra properties of the choice.
347 */
348 extra?: any;
349 }
350
351 /**
352 * Provides options for a choice of the `ListPrompt`.
353 *
354 * @template T
355 * The type of the answers.
356 */
357 interface ListChoiceOptions<T extends Answers = Answers> extends ChoiceOptions<T> {
358 /**
359 * A value indicating whether the choice is disabled.
360 */
361 disabled?: DynamicQuestionProperty<boolean | string, T>;
362 }
363
364 /**
365 * Provides options for a choice of the `CheckboxPrompt`.
366 *
367 * @template T
368 * The type of the answers.
369 */
370 interface CheckboxChoiceOptions<T extends Answers = Answers> extends ListChoiceOptions<T> {
371 /**
372 * A value indicating whether the choice should be initially checked.
373 */
374 checked?: boolean;
375 }
376
377 /**
378 * Provides options for a choice of the `ExpandPrompt`.
379 *
380 * @template T
381 * The type of the answers.
382 */
383 interface ExpandChoiceOptions<T extends Answers = Answers> extends ChoiceOptions<T> {
384 /**
385 * The key to press for selecting the choice.
386 */
387 key?: string;
388 }
389
390 /**
391 * Represents a separator.
392 */
393 interface SeparatorOptions {
394 /**
395 * Gets the type of the choice.
396 */
397 type: "separator";
398
399 /**
400 * Gets or sets the text of the separator.
401 */
402 line?: string;
403 }
404
405 /**
406 * Provides all valid choice-types for any kind of question.
407 *
408 * @template T
409 * The type of the answers.
410 */
411 interface BaseChoiceMap<T extends Answers = Answers> {
412 Choice: Choice<T>;
413 ChoiceOptions: ChoiceOptions<T>;
414 SeparatorOptions: SeparatorOptions;
415 Separator: Separator;
416 }
417
418 /**
419 * Provides all valid choice-types for the `ListQuestion`.
420 *
421 * @template T
422 * The type of the answers.
423 */
424 interface ListChoiceMap<T extends Answers = Answers> extends BaseChoiceMap<T> {
425 ListChoiceOptions: ListChoiceOptions<T>;
426 }
427
428 /**
429 * Provides all valid choice-types for the `CheckboxQuestion`.
430 *
431 * @template T
432 * The type of the answers.
433 */
434 interface CheckboxChoiceMap<T extends Answers = Answers> extends BaseChoiceMap<T> {
435 CheckboxChoiceOptions: CheckboxChoiceOptions<T>;
436 }
437
438 /**
439 * Provides all valid choice-types for the `ExpandQuestion`.
440 *
441 * @template T
442 * The type of the answers.
443 */
444 interface ExpandChoiceMap<T extends Answers = Answers> extends BaseChoiceMap<T> {
445 ExpandChoiceOptions: ExpandChoiceOptions<T>;
446 }
447
448 /**
449 * Provides all valid choice-types.
450 *
451 * @template T
452 * The type of the answers.
453 */
454 interface AllChoiceMap<T extends Answers = Answers> {
455 BaseChoiceMap: BaseChoiceMap<T>[keyof BaseChoiceMap<T>];
456 ListChoiceMap: ListChoiceMap<T>[keyof ListChoiceMap<T>];
457 CheckboxChoiceMap: CheckboxChoiceMap<T>[keyof CheckboxChoiceMap<T>];
458 ExpandChoiceMap: ExpandChoiceMap<T>[keyof ExpandChoiceMap<T>];
459 }
460
461 /**
462 * Provides valid choices for the question of the `TChoiceMap`.
463 *
464 * @template TChoiceMap
465 * The choice-types to provide.
466 */
467 type DistinctChoice<TChoiceMap> =
468 string |
469 TChoiceMap[keyof TChoiceMap];
470
471 /**
472 * Represents a set of choices.
473 */
474 type ChoiceCollection<T extends Answers = Answers> = Array<DistinctChoice<AllChoiceMap>>;
475
476 /**
477 * Provides options for a question for the `InputPrompt`.
478 *
479 * @template T
480 * The type of the answers.
481 */
482 interface InputQuestionOptions<T extends Answers = Answers> extends Question<T> {
483 /**
484 * Transforms the value to display to the user.
485 *
486 * @param input
487 * The input provided by the user.
488 *
489 * @param answers
490 * The answers provided by the users.
491 *
492 * @param flags
493 * Additional information about the value.
494 *
495 * @returns
496 * The value to display to the user.
497 */
498 transformer?(input: any, answers: T, flags: { isFinal?: boolean }): string | Promise<string>;
499 }
500
501 /**
502 * Provides options for a question for the `InputPrompt`.
503 *
504 * @template T
505 * The type of the answers.
506 */
507 interface InputQuestion<T extends Answers = Answers> extends InputQuestionOptions<T> {
508 /**
509 * @inheritdoc
510 */
511 type?: "input";
512 }
513
514 /**
515 * Provides options for a question for the `NumberPrompt`.
516 *
517 * @template T
518 * The type of the answers.
519 */
520 interface NumberQuestionOptions<T extends Answers = Answers> extends InputQuestionOptions<T> { }
521
522 /**
523 * Provides options for a question for the `NumberPrompt`.
524 *
525 * @template T
526 * The type of the answers.
527 */
528 interface NumberQuestion<T extends Answers = Answers> extends NumberQuestionOptions<T> {
529 /**
530 * @inheritdoc
531 */
532 type: "number";
533 }
534
535 /**
536 * Provides options for a question for the `PasswordPrompt`.
537 *
538 * @template T
539 * The type of the answers.
540 */
541 interface PasswordQuestionOptions<T extends Answers = Answers> extends InputQuestionOptions<T> {
542 /**
543 * The character to replace the user-input.
544 */
545 mask?: string;
546 }
547
548 /**
549 * Provides options for a question for the `PasswordPrompt`.
550 *
551 * @template T
552 * The type of the answers.
553 */
554 interface PasswordQuestion<T extends Answers = Answers> extends PasswordQuestionOptions<T> {
555 /**
556 * @inheritdoc
557 */
558 type: "password";
559 }
560
561 /**
562 * Provides options for a question for the `ListPrompt`.
563 *
564 * @template T
565 * The type of the answers.
566 */
567 interface ListQuestionOptions<T extends Answers = Answers> extends ListQuestionOptionsBase<T, ListChoiceMap<T>> {
568 /**
569 * A value indicating whether choices in a list should be looped.
570 */
571 loop?: boolean;
572 }
573
574 /**
575 * Provides options for a question for the `ListPrompt`.
576 *
577 * @template T
578 * The type of the answers.
579 */
580 interface ListQuestion<T extends Answers = Answers> extends ListQuestionOptions<T> {
581 /**
582 * @inheritdoc
583 */
584 type: "list";
585 }
586
587 /**
588 * Provides options for a question for the `RawListPrompt`.
589 *
590 * @template T
591 * The type of the answers.
592 */
593 interface RawListQuestionOptions<T extends Answers = Answers> extends ListQuestionOptions<T> { }
594
595 /**
596 * Provides options for a question for the `RawListPrompt`.
597 *
598 * @template T
599 * The type of the answers.
600 */
601 interface RawListQuestion<T extends Answers = Answers> extends RawListQuestionOptions<T> {
602 /**
603 * @inheritdoc
604 */
605 type: "rawlist";
606 }
607
608 /**
609 * Provides options for a question for the `ExpandPrompt`.
610 *
611 * @template T
612 * The type of the answers.
613 */
614 interface ExpandQuestionOptions<T extends Answers = Answers> extends ListQuestionOptionsBase<T, ExpandChoiceMap<T>> { }
615
616 /**
617 * Provides options for a question for the `ExpandPrompt`.
618 *
619 * @template T
620 * The type of the answers.
621 */
622 interface ExpandQuestion<T extends Answers = Answers> extends ExpandQuestionOptions<T> {
623 /**
624 * @inheritdoc
625 */
626 type: "expand";
627 }
628
629 /**
630 * Provides options for a question for the `CheckboxPrompt`.
631 *
632 * @template T
633 * The type of the answers.
634 */
635 interface CheckboxQuestionOptions<T extends Answers = Answers> extends ListQuestionOptionsBase<T, CheckboxChoiceMap<T>> { }
636
637 /**
638 * Provides options for a question for the `CheckboxPrompt`.
639 *
640 * @template T
641 * The type of the answers.
642 */
643 interface CheckboxQuestion<T extends Answers = Answers> extends CheckboxQuestionOptions<T> {
644 /**
645 * @inheritdoc
646 */
647 type: "checkbox";
648 }
649
650 /**
651 * Provides options for a question for the `ConfirmPrompt`.
652 *
653 * @template T
654 * The type of the answers.
655 */
656 interface ConfirmQuestionOptions<T extends Answers = Answers> extends Question<T> { }
657
658 /**
659 * Provides options for a question for the `ConfirmPrompt`.
660 *
661 * @template T
662 * The type of the answers.
663 */
664 interface ConfirmQuestion<T extends Answers = Answers> extends ConfirmQuestionOptions<T> {
665 /**
666 * @inheritdoc
667 */
668 type: "confirm";
669 }
670
671 /**
672 * Provides options for a question for the `EditorPrompt`.
673 *
674 * @template T
675 * The type of the answers.
676 */
677 interface EditorQuestionOptions<T extends Answers = Answers> extends Question<T> { }
678
679 /**
680 * Provides options for a question for the `EditorPrompt`.
681 *
682 * @template T
683 * The type of the answers.
684 */
685 interface EditorQuestion<T extends Answers = Answers> extends EditorQuestionOptions<T> {
686 /**
687 * @inheritdoc
688 */
689 type: "editor";
690 }
691
692 /**
693 * Provides the available question-types.
694 *
695 * @template T
696 * The type of the answers.
697 */
698 interface QuestionMap<T extends Answers = Answers> {
699 /**
700 * The `InputQuestion` type.
701 */
702 input: InputQuestion<T>;
703
704 /**
705 * The `NumberQuestion` type.
706 */
707 number: NumberQuestion<T>;
708
709 /**
710 * The `PasswordQuestion` type.
711 */
712 password: PasswordQuestion<T>;
713
714 /**
715 * The `ListQuestion` type.
716 */
717 list: ListQuestion<T>;
718
719 /**
720 * The `RawListQuestion` type.
721 */
722 rawList: RawListQuestion<T>;
723
724 /**
725 * The `ExpandQuestion` type.
726 */
727 expand: ExpandQuestion<T>;
728
729 /**
730 * The `CheckboxQuestion` type.
731 */
732 checkbox: CheckboxQuestion<T>;
733
734 /**
735 * The `ConfirmQuestion` type.
736 */
737 confirm: ConfirmQuestion<T>;
738
739 /**
740 * The `EditorQuestion` type.
741 */
742 editor: EditorQuestion<T>;
743 }
744
745 /**
746 * Represents one of the available questions.
747 *
748 * @template T
749 * The type of the answers.
750 */
751 type DistinctQuestion<T extends Answers = Answers> = QuestionMap<T>[keyof QuestionMap<T>];
752
753 /**
754 * Indicates the type of a question
755 */
756 type QuestionTypeName = DistinctQuestion["type"];
757
758 /**
759 * Represents a collection of questions.
760 *
761 * @template T
762 * The type of the answers.
763 */
764 type QuestionCollection<T extends Answers = Answers> =
765 | DistinctQuestion<T>
766 | ReadonlyArray<DistinctQuestion<T>>
767 | Observable<DistinctQuestion<T>>;
768
769 /**
770 * Provides components for the prompts.
771 */
772 namespace prompts {
773 /**
774 * Provides a base for and prompt-options.
775 *
776 * @template T
777 * The type of the answers.
778 */
779 type PromptOptions<T extends Question = Question> = T & {
780 /**
781 * The choices of the prompt.
782 */
783 choices: Choices;
784 };
785
786 /**
787 * Represents the state of a prompt.
788 */
789 type PromptState = LiteralUnion<"pending" | "idle" | "loading" | "answered" | "done">;
790
791 /**
792 * Represents a prompt.
793 */
794 interface PromptBase {
795 /**
796 * Gets or sets a string which represents the state of the prompt.
797 */
798 status: PromptState;
799
800 /**
801 * Runs the prompt.
802 *
803 * @returns
804 * The result of the prompt.
805 */
806 run(): Promise<any>;
807 }
808
809 /**
810 * Provides the functionality to initialize new prompts.
811 */
812 interface PromptConstructor {
813 /**
814 * Initializes a new instance of a prompt.
815 *
816 * @param question
817 * The question to prompt.
818 *
819 * @param readLine
820 * An object for reading from the command-line.
821 *
822 * @param answers
823 * The answers provided by the user.
824 */
825 new(question: any, readLine: ReadlineInterface, answers: Answers): PromptBase;
826 }
827
828 /**
829 * Provides a set of prompt-constructors.
830 */
831 type PromptCollection = Record<string, PromptConstructor>;
832
833 /**
834 * Provides data about the state of a prompt.
835 */
836 interface PromptStateData {
837 /**
838 * Either a string which describes the error of the prompt or a boolean indicating whether the prompt-value is valid.
839 */
840 isValid: string | boolean;
841 }
842
843 /**
844 * Provides data about the successful state of a prompt.
845 *
846 * @param T
847 * The type of the answer.
848 */
849 interface SuccessfulPromptStateData<T = any> extends PromptStateData {
850 /**
851 * @inheritdoc
852 */
853 isValid: true;
854
855 /**
856 * The value of the prompt.
857 */
858 value: T;
859 }
860
861 /**
862 * Provides data about the failed state of a prompt.
863 */
864 interface FailedPromptStateData extends PromptStateData {
865 /**
866 * @inheritdoc
867 */
868 isValid: false | string;
869 }
870
871 /**
872 * Provides pipes for handling events of a prompt.
873 *
874 * @param T
875 * The type of the answer.
876 */
877 interface PromptEventPipes<T = any> {
878 /**
879 * A pypeline for succesful inputs.
880 */
881 success: Observable<SuccessfulPromptStateData<T>>;
882
883 /**
884 * An object representing an error.
885 */
886 error: Observable<FailedPromptStateData>;
887 }
888 }
889
890 /**
891 * Provides components for the ui.
892 */
893 namespace ui {
894 /**
895 * Provides options for the bottom-bar UI.
896 */
897 interface BottomBarOptions extends StreamOptions {
898 /**
899 * The initial text to display.
900 */
901 bottomBar?: string;
902 }
903
904 /**
905 * Represents a fetched answer.
906 *
907 * @template T
908 * The type of the answers.
909 */
910 type FetchedQuestion<T extends Answers = Answers> = DistinctQuestion<T> & {
911 /**
912 * The type of the question.
913 */
914 type: string;
915
916 /**
917 * The message to show to the user.
918 */
919 message: string;
920
921 /**
922 * The default value of the question.
923 */
924 default: any;
925
926 /**
927 * The choices of the question.
928 */
929 choices: ChoiceCollection<T>;
930 };
931
932 /**
933 * Represents a fetched answer.
934 */
935 interface FetchedAnswer {
936 /**
937 * The name of the answer.
938 */
939 name: string;
940
941 /**
942 * The value of the answer.
943 */
944 answer: any;
945 }
946 }
947}
948
949/**
950 * Provides the functionality to prompt questions.
951 */
952declare var inquirer: inquirer.Inquirer;
953export = inquirer;
954
\No newline at end of file