From 8d7f6a54bcdd1c4835a3e3019cc88e1c1e73bcfa Mon Sep 17 00:00:00 2001 From: Jason Dreyzehner Date: Mon, 5 Mar 2018 23:12:18 -0500 Subject: [PATCH] inquirer: array params are not required to be mutable --- types/inquirer/index.d.ts | 22 ++++++++++++++++------ types/inquirer/inquirer-tests.ts | 21 +++++++++++++++++++++ 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/types/inquirer/index.d.ts b/types/inquirer/index.d.ts index 32c54fa99e..22157ecce0 100644 --- a/types/inquirer/index.d.ts +++ b/types/inquirer/index.d.ts @@ -3,7 +3,9 @@ // Definitions by: Qubo // Parvez // Jouderian +// Jason Dreyzehner // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.1 /// @@ -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; + type Questions = + | Question + | ReadonlyArray + | Rx.Observable; 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 + | ((answers: Answers) => ReadonlyArray); /** * 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, + answers?: Answers + ): Choices; + choices: ReadonlyArray; + realChoices: ReadonlyArray; length: number; realLength: number; /** diff --git a/types/inquirer/inquirer-tests.ts b/types/inquirer/inquirer-tests.ts index 4a43dcc98e..1afce2e378 100644 --- a/types/inquirer/inquirer-tests.ts +++ b/types/inquirer/inquirer-tests.ts @@ -525,3 +525,24 @@ function likesFood ( aFood: string ) { inquirer.prompt(questions, function (answers) { console.log( JSON.stringify(answers, null, " ") ); }); + +// immutable usage +const immutableChoices: ReadonlyArray = [ + '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, ' ')); + } +);