Giter Site home page Giter Site logo

TypeScript support about prompts HOT 13 CLOSED

terkelg avatar terkelg commented on August 24, 2024 1
TypeScript support

from prompts.

Comments (13)

armand1m avatar armand1m commented on August 24, 2024 5

Hey, I came with this for now:

declare module "prompts" {
    export = PromptFn;
}

declare function PromptFn (
    prompts: Prompt.PromptParameter | Array<Prompt.PromptParameter>,
    options?: Prompt.PromptOptions,
): Promise<Object>;

declare namespace Prompt {
    interface PromptOptions {
        onCancel: Function;
        onSubmit: Function;
    }
    
    type PromptParameter = Question |
        TextQuestion |
        PasswordQuestion |  
        InvisibleQuestion |
        NumberQuestion |
        ListQuestion |
        ToggleQuestion |
        SelectQuestion |
        MultiSelectQuestion |
        AutoCompleteQuestion;

    interface Choice {
        title: string;
        value: string | any;
        selected: boolean;
    }
    
    interface Question {
        type: string | Function;
        name: string | Function;
        message: string | Function;
        initial?: string | number | boolean | Function;
        format?: Function;
        onState?: Function;
    }
    
    interface HasStyle {
        style: string;
    }
    
    interface HasMaxControl {
        max: number;
    }
    
    interface HasMinControl {
        min: number;
    }
    
    interface HasSeparator {
        separator: string;
    }
    
    interface HasToggleControl {
        active: string;
        inactive: string;
    }
    
    interface HasChoices {
        choices: Array<Choice>;
    }
    
    interface HasHint {
        hint: string;
    }
    
    interface HasSuggestions {
        suggest: Function;
    }
    
    interface HasLimit {
        limit: number;
    }
    
    interface TextQuestion extends Question, HasStyle {}
    interface PasswordQuestion extends Question {}
    interface InvisibleQuestion extends Question {}
    interface NumberQuestion extends Question, HasStyle, HasMaxControl, HasMinControl {}
    interface ListQuestion extends Question, HasSeparator {}
    interface ToggleQuestion extends Question, HasToggleControl {}
    interface SelectQuestion extends Question, HasChoices {}
    interface MultiSelectQuestion extends Question, HasChoices, HasMaxControl, HasHint {}
    interface AutoCompleteQuestion extends Question, HasChoices, HasStyle, HasLimit {}
}

I guess it needs to be published in the DefinitelyTyped repo :)

from prompts.

Hotell avatar Hotell commented on August 24, 2024 4

if there is not a dedicated author responsible for maintaining ambient types, the repo of actual package is not the right place for types to live in. DefinitelyType is the right place in this case. Prompts is already there, so please feel free to contribute over there folks.

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/prompts/index.d.ts

thanks!

from prompts.

terkelg avatar terkelg commented on August 24, 2024 2

Thanks @Hotell

from prompts.

Hotell avatar Hotell commented on August 24, 2024 2

I guess this should be handled(issue or PR) at definitelyTypes repo. If the api definition doesn't match implementation please submit PR there . cheers

from prompts.

terkelg avatar terkelg commented on August 24, 2024 1

Perfect! I know nothing about what's best practice when it comes to Flow and TypeScript. Not even sure how to test it but I trust you guys. There's a few new features I haven't documented yet #55

from prompts.

saadq avatar saadq commented on August 24, 2024 1

Cool, I'll wait until that is documented. And, normally we submit a test file alongside the type definitions (I assume @armand1m was planning on doing it as well) that make sure the types work as expected. As reference, here is a pull request I submitted to chalk to add a flow libdef.

from prompts.

terkelg avatar terkelg commented on August 24, 2024

I don't use TypeScript myself, but I'm open to include a definitions file if somebody wants to make a PR

from prompts.

armand1m avatar armand1m commented on August 24, 2024

Or we can ship this with the project? If so I can prepare a pull request as well =)

from prompts.

saadq avatar saadq commented on August 24, 2024

Kind of related, I had started working on Flow type defs for this awhile back, need to check if they need to be updated.

// @flow

type Value = string | boolean | number;

type Values = {
  [key: string]: Value | Array<Value>
};

type RenderStyle = 'default' | 'password' | 'invisible';

type Choice = {|
  title: string,
  value: Value
|};

type MultiSelectChoice = {|
  ...Choice,
  selected?: boolean
|};

type PromptType =
  | null
  | 'text'
  | 'password'
  | 'invisible'
  | 'number'
  | 'confirm'
  | 'list'
  | 'toggle'
  | 'select'
  | 'multiselect'
  | 'autocomplete';

type PromptFuncType = (
  prev: Value,
  values: Values,
  prompt: Prompt
) => PromptType;

type Prompt = {|
  type?: PromptType | PromptFuncType,
  name?: string | PromptFuncType,
  message?: string | PromptFuncType,
  initial?: string | PromptFuncType,
  format?: (answer: Value, answers: Values) => Value
|};

type Options = {|
  onSubmit?: () => void | true,
  onCancel?: () => void | true
|};

declare var Prompts: {|
  (questions: Prompt | Array<Prompt>, opts?: Options): Promise<Values>,

  prompt: (
    questions: Prompt | Array<Prompt>,
    opts?: Options
  ) => Promise<Values>,

  prompts: {|
    text: (
      message: string,
      initial?: string,
      style?: RenderStyle
    ) => Promise<Value>,

    password: (
      message: string,
      initial?: string
    ) => Promise<Value>,

    invisible: (
      message: string,
      initial?: string
    ) => Promise<Value>,

    number: (
      message: string,
      initial: number,
      max?: number,
      min?: number,
      style?: RenderStyle
    ) => Promise<Value>,

    confirm: (
      message: string,
      initial?: boolean
    ) => Promise<Value>,

    list: (
      message: string,
      initial?: string,
      style?: RenderStyle,
      separator?: string
    ) => Promise<Value>,

    toggle: (
      message: string,
      initial?: boolean,
      active?: string,
      inactive?: string
    ) => Promise<Value>,

    select: (
      message: string,
      choices: Array<Object>,
      initial?: number
    ) => Promise<Value>,

    multiselect: (
      message: string,
      choices: Array<MultiSelectChoice>,
      max?: number,
      hint?: string
    ) => Promise<Value>,

    autocomplete: (
      message: string,
      choices: Array<Object>,
      suggest?: (input: string, choices: Array<Choice>) => Promise<Array<Choice>>,
      limit?: number,
      style?: RenderStyle
    ) => Promise<Value>
  |},
|};

module.exports = Prompts;

I'd also like to know if you would be willing to include the flow libdef in the project, or I can just publish them to flow-typed. Didn't want to make a separate issue about it.

from prompts.

armand1m avatar armand1m commented on August 24, 2024

@saadq I'll borrow some types from your flow configuration such as the MultiSelectChoice before submitting a pull request here. Awesome contribution you made.

I may add node-ts as a dependency in this project just to run a little test suite under the developed interfaces to assure they are respecting the contract. Does this sounds ok?

from prompts.

madhavarshney avatar madhavarshney commented on August 24, 2024

@terkelg - I'm interested in contributing to add TypeScript support. Please let me know if that's fine with you 👍 .

from prompts.

terkelg avatar terkelg commented on August 24, 2024

I'd love that! Feel free to go ahead. Maybe even help with the missing documentation if possible #55 . I'm a bit busy right now

from prompts.

Zikoel avatar Zikoel commented on August 24, 2024

@Hotell thank you! Where is the right place where talk about prompts types? I have the following question:
Why the funcion function toggle(args: PromptObject): void; returns void and not boolean? There is a reason?
Maybe better open an issue on DefinitelyTyped repo ?

from prompts.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.