inquirer: array params are not required to be mutable

This commit is contained in:
Jason Dreyzehner
2018-03-05 23:12:18 -05:00
parent fbba9f55be
commit 8d7f6a54bc
2 changed files with 37 additions and 6 deletions
+16 -6
View File
@@ -3,7 +3,9 @@
// Definitions by: Qubo <https://github.com/tkQubo>
// Parvez <https://github.com/ppathan>
// Jouderian <https://github.com/jouderianjr>
// Jason Dreyzehner <https://github.com/bitjson>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.1
/// <reference types="rx" />
@@ -12,7 +14,10 @@ import through = require('through');
declare namespace inquirer {
type Prompts = { [name: string]: PromptModule };
type ChoiceType = string | objects.ChoiceOption | objects.Separator;
type Questions = Question | Question[] | Rx.Observable<Question>;
type Questions =
| Question
| ReadonlyArray<Question>
| Rx.Observable<Question>;
interface Inquirer {
restoreDefaultPrompts(): void;
@@ -39,7 +44,7 @@ declare namespace inquirer {
ui: {
BottomBar: ui.BottomBar;
Prompt: ui.Prompt;
}
};
}
interface PromptModule {
@@ -91,7 +96,9 @@ declare namespace inquirer {
* Array values can be simple strings, or objects containing a name (to display) and a value properties
* (to save in the answers hash). Values can also be a Separator.
*/
choices?: ChoiceType[] | ((answers: Answers) => ChoiceType[]);
choices?:
| ReadonlyArray<ChoiceType>
| ((answers: Answers) => ReadonlyArray<ChoiceType>);
/**
* Receive the user input and should return true if the value is valid, and an error message (String)
* otherwise. If false is returned, a default error message is provided.
@@ -247,9 +254,12 @@ declare namespace inquirer {
* @param choices All `choice` to keep in the collection
*/
interface Choices {
new (choices: (string | Separator | ChoiceOption)[], answers?: Answers): Choices;
choices: Choice[];
realChoices: Choice[];
new (
choices: ReadonlyArray<string | Separator | ChoiceOption>,
answers?: Answers
): Choices;
choices: ReadonlyArray<Choice>;
realChoices: ReadonlyArray<Choice>;
length: number;
realLength: number;
/**
+21
View File
@@ -525,3 +525,24 @@ function likesFood ( aFood: string ) {
inquirer.prompt(questions, function (answers) {
console.log( JSON.stringify(answers, null, " ") );
});
// immutable usage
const immutableChoices: ReadonlyArray<string> = [
'Order a pizza',
'Make a reservation',
'Ask opening hours',
'Talk to the receptionist'
];
inquirer.prompt(
[
{
type: 'rawlist',
name: 'theme',
message: 'What do you want to do?',
choices: immutableChoices
}
],
function(answers: inquirer.Answers) {
console.log(JSON.stringify(answers, null, ' '));
}
);