UNPKG

3.28 kBTypeScriptView Raw
1import { checkbox, confirm, editor, expand, input, number, password, rawlist, search, select } from '@inquirer/prompts';
2import type { Context, DistributiveMerge, Prettify } from '@inquirer/type';
3import { Observable } from 'rxjs';
4export type Answers<Key extends string = string> = Record<Key, any>;
5type AsyncCallbackFunction<R> = (...args: [error: null | undefined, value: R] | [error: Error, value: undefined]) => void;
6export type AsyncGetterFunction<R, A extends Answers> = (this: {
7 async: () => AsyncCallbackFunction<R>;
8}, answers: Prettify<Partial<A>>) => void | R | Promise<R>;
9/**
10 * Allows to inject a custom question type into inquirer module.
11 *
12 * @example
13 * ```ts
14 * declare module 'inquirer' {
15 * interface QuestionMap {
16 * custom: { message: string };
17 * }
18 * }
19 * ```
20 *
21 * Globally defined question types are not correct.
22 */
23export interface QuestionMap {
24 __dummy: {
25 message: string;
26 };
27}
28type KeyValueOrAsyncGetterFunction<T, k extends string, A extends Answers> = T extends Record<string, any> ? T[k] | AsyncGetterFunction<T[k], A> : never;
29export type Question<A extends Answers = Answers, Type extends string = string> = {
30 type: Type;
31 name: string;
32 message: string | AsyncGetterFunction<string, A>;
33 default?: any;
34 choices?: any;
35 filter?: (answer: any, answers: Partial<A>) => any;
36 askAnswered?: boolean;
37 when?: boolean | AsyncGetterFunction<boolean, A>;
38};
39type QuestionWithGetters<Type extends string, Q extends Record<string, any>, A extends Answers> = DistributiveMerge<Q, {
40 type: Type;
41 askAnswered?: boolean;
42 when?: boolean | AsyncGetterFunction<boolean, A>;
43 filter?(input: any, answers: A): any;
44 message: KeyValueOrAsyncGetterFunction<Q, 'message', A>;
45 default?: KeyValueOrAsyncGetterFunction<Q, 'default', A>;
46 choices?: KeyValueOrAsyncGetterFunction<Q, 'choices', A>;
47}>;
48export type UnnamedDistinctQuestion<A extends Answers = object> = QuestionWithGetters<'checkbox', Parameters<typeof checkbox>[0], A> | QuestionWithGetters<'confirm', Parameters<typeof confirm>[0], A> | QuestionWithGetters<'editor', Parameters<typeof editor>[0], A> | QuestionWithGetters<'expand', Parameters<typeof expand>[0], A> | QuestionWithGetters<'input', Parameters<typeof input>[0], A> | QuestionWithGetters<'number', Parameters<typeof number>[0], A> | QuestionWithGetters<'password', Parameters<typeof password>[0], A> | QuestionWithGetters<'rawlist', Parameters<typeof rawlist>[0], A> | QuestionWithGetters<'search', Parameters<typeof search>[0], A> | QuestionWithGetters<'list', Parameters<typeof select>[0], A> | QuestionWithGetters<'select', Parameters<typeof select>[0], A>;
49export type DistinctQuestion<A extends Answers = Answers> = Prettify<UnnamedDistinctQuestion<A> & {
50 name: Extract<keyof A, string>;
51}>;
52export type CustomQuestion<A extends Answers, Q extends Record<string, Record<string, any>>> = {
53 [key in Extract<keyof Q, string>]: Readonly<QuestionWithGetters<key, Q[key], A>>;
54}[Extract<keyof Q, string>];
55export type PromptSession<A extends Answers = Answers, Q extends Question<A> = Question<A>> = Q[] | Record<string, Omit<Q, 'name'>> | Observable<Q> | Q;
56export type StreamOptions = Prettify<Context & {
57 skipTTYChecks?: boolean;
58}>;
59export {};