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

prompt() questions input type is incompatible with @yeoman/adapter #1575

Closed
jmuzina opened this issue Dec 9, 2024 · 5 comments · Fixed by #1578
Closed

prompt() questions input type is incompatible with @yeoman/adapter #1575

jmuzina opened this issue Dec 9, 2024 · 5 comments · Fixed by #1578
Labels
needs triage Awaiting triage

Comments

@jmuzina
Copy link
Contributor

jmuzina commented Dec 9, 2024

Calling Generator.prompt() with an array of questions of type PromptQuestions throws a type error.

import type { PromptAnswers, PromptQuestions } from "@yeoman/types";
import Generator from "yeoman-generator";

interface ComponentGeneratorAnswers extends PromptAnswers {
  // answer types here...
}

export default class ComponentGenerator extends Generator {
  answers?: ComponentGeneratorAnswers;
  questions: PromptQuestions<ComponentGeneratorAnswers> = [];

  async prompting() {
    this.answers = await this.prompt<ComponentGeneratorAnswers>(this.questions);
  }
}
Argument of type
import("node_modules/@yeoman/ adapter/ types/ adapter", {with: {"resolution-mode": "import"}}).PromptQuestions<ComponentGeneratorAnswers>
is not assignable to parameter of type
import("node_modules/ yeoman-generator/ dist/ questions", {with: {"resolution-mode": "import"}}).PromptQuestions<ComponentGeneratorAnswers>
Type NumberQuestion<ComponentGeneratorAnswers> is not assignable to type PromptQuestions<ComponentGeneratorAnswers>
Type NumberQuestion<ComponentGeneratorAnswers> is not assignable to type
NumberQuestion<ComponentGeneratorAnswers> & {   name: string;   storage?: Storage | undefined;   store?: boolean | undefined; }
Type NumberQuestion<ComponentGeneratorAnswers> is not assignable to type
{   name: string;   storage?: Storage | undefined;   store?: boolean | undefined; }
Types of property name are incompatible.
Type string | undefined is not assignable to type string
Type undefined is not assignable to type string

Directly passing an array of questions into prompt() is valid:

  interface ComponentGeneratorAnswers extends PromptAnswers {
    myQuestion: string
  }
  // ....
  async prompting() {
    this.answers = await this.prompt<ComponentGeneratorAnswers>([
      {
        name: "myQuestion",
        type: "input",
      },
    ]);
  }

But storing them in a variable is not:

  interface ComponentGeneratorAnswers extends PromptAnswers {
    myQuestion: string
  }
  // ....
  async prompting() {
    // Throws an error
    this.answers = await this.prompt<ComponentGeneratorAnswers>(this.questions);
  }

Am I doing something wrong storing these prompt questions into an attribute? I'd like to store them this way so i can do more with them than just prompt interactive questions (such as create CLI arguments).

@jmuzina jmuzina added the needs triage Awaiting triage label Dec 9, 2024
@jmuzina
Copy link
Contributor Author

jmuzina commented Dec 9, 2024

I believe I've found the source of this issue.

The yeoman-generator PromptQuestions type declares name as required:

export type PromptQuestion<T extends PromptAnswers = PromptAnswers> = PromptQuestionApi<T> & {
name: string;

@types/inquirer declares the name key as optional:
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/e777b97e68ff2f3609772d29e44361d501b0c25c/types/inquirer/index.d.ts#L359

@yeoman/adapter, which is re-exported by @yeoman/types, re-exports the inquirer types here:
https://github.com/yeoman/yeoman-api/blob/3b77ad9325d6ab8cb4bcb2d42580ed7988b552bb/workspaces/adapter/types/adapter.d.ts#L9

This means that the Yeoman adapter and Yeoman types are treating name as optional, just like Inquirer does, but the prompt() function in yeoman-generator treats name as required.

Possibly a modification could be made to @yeoman/adapter to fix the issue:

export type PromptQuestion<A extends PromptAnswers = PromptAnswers> = DistinctQuestion<A> & { name: string };

@jmuzina
Copy link
Contributor Author

jmuzina commented Dec 9, 2024

I've created a PR for @yeoman/adapter to try and fix the issue. Maybe this issue should be transferred to yeoman-api

@mshima
Copy link
Member

mshima commented Dec 10, 2024

Generator's types should be used instead.
https://github.com/yeoman/generator/blob/main/src/questions.d.ts

Maybe it’s not exported yet.

@jmuzina
Copy link
Contributor Author

jmuzina commented Dec 10, 2024

Generator's types should be used instead. https://github.com/yeoman/generator/blob/main/src/questions.d.ts

Maybe it’s not exported yet.

@mshima Yes, PromptQuestion and similar types from questions.d.ts are not exported by this package. I can raise a PR that exports them if you like.

@mshima
Copy link
Member

mshima commented Dec 10, 2024

Please go ahead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage Awaiting triage
Projects
None yet
2 participants