Skip to content

TypeScript: Infer prompt() return type object shape from question objects #463

Open
@karlhorky

Description

Hi, first of all, thanks for enquirer 👍 Nice library!

Currently, enquirer has a return type of Promise<object>:

import { prompt } from 'enquirer';

const questions = [
  {
    type: 'input',
    name: 'name',
    message: 'What is your name?',
  },
  {
    type: 'input',
    name: 'username',
    message: 'What is your username?',
  },
];
const result = await prompt(questions);
   // ^? const result: object

Playground

It would be great if prompt() would infer the types from the questions object:

import { prompt } from 'enquirer';

const questions = [
  {
    type: 'input',
    name: 'name',
    message: 'What is your name?',
  },
  {
    type: 'input',
    name: 'username',
    message: 'What is your username?',
  },
];
const result = await prompt(questions);
   // ^? const result: { name: string; username: string; }

Alternatives considered

Pass in generics, this works today, but requires careful synchronization of the type and the questions - easy to make mistakes:

import { prompt } from 'enquirer';

type Result = {
  name: string;
  username: string;
};

const questions = [
  {
    type: 'input',
    name: 'name',
    message: 'What is your name?',
  },
  {
    type: 'input',
    name: 'username',
    message: 'What is your username?',
  },
];
const result = await prompt<Result>(questions);
   // ^? const result: { name: string; username: string; }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions