Comments (13)
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.
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.
Thanks @Hotell
from prompts.
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.
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.
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.
I don't use TypeScript myself, but I'm open to include a definitions file if somebody wants to make a PR
from prompts.
Or we can ship this with the project? If so I can prepare a pull request as well =)
from prompts.
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.
@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.
@terkelg - I'm interested in contributing to add TypeScript support. Please let me know if that's fine with you 👍 .
from prompts.
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.
@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)
- Ensure types are exported HOT 3
- How to cancel these unnecessary logs HOT 5
- logic diagram
- Ast prompts
- Roadmap for a 3.0 HOT 5
- prompts captures Ctrl-W "cut word" shortcut as control character
- How to make autocomplete required or select searchable?
- Add mask option to Text Type
- Somewhat different published module than the one in the repo HOT 2
- Keypress events may not be triggered on Windows if validate callback contains async actions HOT 2
- Bun support HOT 11
- confirm element: pressing a function key throws an error
- correctly type return/resolved values instead of `any`
- Initial value not passed to validate function for `"number"` types
- When the type is multiSelect, options cannot be switched through the arrows on the keyboard HOT 1
- Better index selection when providing initial for autocomplete
- Ability to prefill input on autocomplete HOT 1
- Can't seem to create a prompt with a Discord ID
- autocompleteMultiselect errors on invalid option
- Content "scrolls up" when typing a new value after validation fails
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from prompts.