#362·enquirer

Export individual PromptOptions

Author: eliellisCreated Jul 9, 2021Updated Dec 24, 2024

I'm working on a Typescript application using this library (great btw!) and I'd like to use the PromptOptions types individually (apart from the type union). As the typings are currently written, I can access the PromptOptions type even though it is not publicly exported using conditional types + infer, however the union that is PromptOptions is too wide for typing anything to a single particular option type or taking advantage of structural type inference off of the type key of an option.

Is it possible to modify the typings to export all the prompt option interfaces? It would be much appreciated. As of now, my application is redefining all of these types, however this is obviously not a maintainable approach as this library will continue to change and the types may drift.

The proposed changes to index.d.ts as described can be seen below:

typescript
// index.d.ts
import { EventEmitter } from "events";

export interface BasePromptOptions {
  name: string | (() => string);
  type: string | (() => string);
  message: string | (() => string) | (() => Promise<string>);
  initial?: any;
  required?: boolean;
  format?(value: string): string | Promise<string>;
  result?(value: string): string | Promise<string>;
  skip?: ((state: any) => boolean | Promise<boolean>) | boolean;
  validate?(
    value: string
  ): boolean | Promise<boolean> | string | Promise<string>;
  onSubmit?(
    name: string,
    value: any,
    prompt: Prompt
  ): boolean | Promise<boolean>;
  onCancel?(
    name: string,
    value: any,
    prompt: Prompt
  ): boolean | Promise<boolean>;
  stdin?: NodeJS.ReadStream;
  stdout?: NodeJS.WriteStream;
}

export interface Choice {
  name: string;
  message?: string;
  value?: string;
  hint?: string;
  disabled?: boolean | string;
}

export interface ArrayPromptOptions extends BasePromptOptions {
  type:
    | 'autocomplete'
    | 'editable'
    | 'form'
    | 'multiselect'
    | 'select'
    | 'survey'
    | 'list'
    | 'scale';
  choices: string[] | Choice[];
  maxChoices?: number;
  muliple?: boolean;
  initial?: number;
  delay?: number;
  separator?: boolean;
  sort?: boolean;
  linebreak?: boolean;
  edgeLength?: number;
  align?: 'left' | 'right';
  scroll?: boolean;
}

export interface BooleanPromptOptions extends BasePromptOptions {
  type: 'confirm';
  initial?: boolean;
}

export interface StringPromptOptions extends BasePromptOptions {
  type: 'input' | 'invisible' | 'list' | 'password' | 'text';
  initial?: string;
  multiline?: boolean;
}

export interface NumberPromptOptions extends BasePromptOptions {
  type: 'numeral';
  min?: number;
  max?: number;
  delay?: number;
  float?: boolean;
  round?: boolean;
  major?: number;
  minor?: number;
  initial?: number;
}

export interface SnippetPromptOptions extends BasePromptOptions {
  type: 'snippet';
  newline?: string;
  template?: string;
}

export interface SortPromptOptions extends BasePromptOptions {
  type: 'sort';
  hint?: string;
  drag?: boolean;
  numbered?: boolean;
}

// ...