Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
karlhorky opened this issue Jun 24, 2024 · 0 comments
Open

Comments

@karlhorky
Copy link

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; }
@karlhorky karlhorky changed the title Infer object shape from question objects TypeScript: Infer prompt() return type object shape from question objects Jun 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant