Skip to content

Commit c3bab7a

Browse files
authored
feat: Improve Inquirer types to infer more valid types (#1880)
1 parent 48d0668 commit c3bab7a

17 files changed

+306
-198
lines changed

packages/inquirer/examples/checkbox.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
* Checkbox list examples
33
*/
44

5-
import inquirer, { type DistinctQuestion } from 'inquirer';
5+
import inquirer from 'inquirer';
66

7-
const answers = await inquirer.prompt([
7+
const answers = await inquirer.prompt<{ toppings: string[] }>([
88
{
99
type: 'checkbox',
1010
message: 'Select toppings',
@@ -27,14 +27,14 @@ const answers = await inquirer.prompt([
2727
{ name: 'Olives', value: 'olives', disabled: true },
2828
{ name: 'Extra cheese', value: 'extra_cheese' },
2929
],
30-
validate(answer) {
30+
validate(answer: string[]) {
3131
if (answer.length === 0) {
3232
return 'You must choose at least one topping.';
3333
}
3434

3535
return true;
3636
},
3737
},
38-
] satisfies DistinctQuestion[]);
38+
]);
3939

4040
console.log(JSON.stringify(answers, null, ' '));

packages/inquirer/examples/editor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Editor prompt example
33
*/
44

5-
import inquirer, { type DistinctQuestion } from 'inquirer';
5+
import inquirer from 'inquirer';
66

77
const answers = await inquirer.prompt([
88
{
@@ -25,6 +25,6 @@ const answers = await inquirer.prompt([
2525
default: 'Hello, World!',
2626
waitForUserInput: false,
2727
},
28-
] satisfies DistinctQuestion[]);
28+
]);
2929

3030
console.log(JSON.stringify(answers, null, ' '));

packages/inquirer/examples/expand.ts

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,17 @@
44

55
import inquirer from 'inquirer';
66

7-
const answers = await inquirer.prompt([
7+
const answers = await inquirer.prompt<{ overwrite: string }>([
88
{
99
type: 'expand',
1010
message: 'Conflict on `file.js`: ',
1111
name: 'overwrite',
1212
choices: [
13-
{
14-
key: 'y',
15-
name: 'Overwrite',
16-
value: 'overwrite',
17-
},
18-
{
19-
key: 'a',
20-
name: 'Overwrite this one and all next',
21-
value: 'overwrite_all',
22-
},
23-
{
24-
key: 'd',
25-
name: 'Show diff',
26-
value: 'diff',
27-
},
13+
{ key: 'y', name: 'Overwrite', value: 'overwrite' },
14+
{ key: 'a', name: 'Overwrite this one and all next', value: 'overwrite_all' },
15+
{ key: 'd', name: 'Show diff', value: 'diff' },
2816
new inquirer.Separator(),
29-
{
30-
key: 'x',
31-
name: 'Abort',
32-
value: 'abort',
33-
},
17+
{ key: 'x', name: 'Abort', value: 'abort' },
3418
],
3519
},
3620
]);

packages/inquirer/examples/filter-validate-progress.ts

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,25 @@
22
* Filter and validate progress example
33
*/
44

5-
import inquirer, { type DistinctQuestion } from 'inquirer';
5+
import inquirer from 'inquirer';
66

77
const answers = await inquirer.prompt([
88
{
99
type: 'input',
10-
name: 'first_question',
11-
message: 'Question with filtering and validating text',
12-
async validate() {
13-
await new Promise((r) => setTimeout(r, 3000));
14-
return true;
15-
},
16-
async filter(answer: string) {
17-
await new Promise((r) => setTimeout(r, 3000));
18-
return `filtered${answer}`;
10+
name: 'name',
11+
message: 'Enter your name',
12+
transformer(input: string, { isFinal }: { isFinal: boolean }) {
13+
if (isFinal) {
14+
return input + '!';
15+
}
16+
return input;
1917
},
2018
},
2119
{
2220
type: 'input',
23-
name: 'second_question',
24-
message: 'Question without filtering and validating text',
25-
async validate() {
26-
await new Promise((r) => setTimeout(r, 3000));
27-
return true;
28-
},
29-
async filter(answer: string) {
30-
await new Promise((r) => setTimeout(r, 3000));
31-
return `filtered${answer}`;
32-
},
21+
name: 'age',
22+
message: 'Enter your age',
3323
},
34-
] satisfies DistinctQuestion[]);
24+
]);
3525

3626
console.log(JSON.stringify(answers, null, ' '));

packages/inquirer/examples/hierarchical.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,18 @@
44

55
import inquirer from 'inquirer';
66

7-
const directionsPrompt: Parameters<typeof inquirer.prompt>[0] = {
8-
type: 'list',
9-
name: 'direction',
10-
message: 'Which direction would you like to go?',
11-
choices: ['Forward', 'Right', 'Left', 'Back'],
12-
};
13-
147
async function main() {
158
console.log('You find youself in a small room, there is a door in front of you.');
169
await exitHouse();
1710
}
1811

1912
async function exitHouse() {
20-
const answers = await inquirer.prompt(directionsPrompt);
13+
const answers = await inquirer.prompt<{ direction: string }>({
14+
type: 'list',
15+
name: 'direction',
16+
message: 'Which direction would you like to go?',
17+
choices: ['Forward', 'Right', 'Left', 'Back'],
18+
});
2119
if (answers['direction'] === 'Forward') {
2220
console.log('You find yourself in a forest');
2321
console.log(
@@ -31,7 +29,12 @@ async function exitHouse() {
3129
}
3230

3331
async function encounter1() {
34-
const answers = await inquirer.prompt(directionsPrompt);
32+
const answers = await inquirer.prompt<{ direction: string }>({
33+
type: 'list',
34+
name: 'direction',
35+
message: 'Which direction would you like to go?',
36+
choices: ['Forward', 'Right', 'Left', 'Back'],
37+
});
3538
if (answers['direction'] === 'Forward') {
3639
console.log('You attempt to fight the wolf');
3740
console.log('Theres a stick and some stones lying around you could use as a weapon');
@@ -47,7 +50,12 @@ async function encounter1() {
4750
}
4851

4952
async function encounter2a() {
50-
const answers = await inquirer.prompt(directionsPrompt);
53+
const answers = await inquirer.prompt<{ direction: string }>({
54+
type: 'list',
55+
name: 'direction',
56+
message: 'Which direction would you like to go?',
57+
choices: ['Forward', 'Right', 'Left', 'Back'],
58+
});
5159
if (answers['direction'] === 'Forward') {
5260
let output = 'You find a painted wooden sign that says:';
5361
output += ' \n';
@@ -63,7 +71,7 @@ async function encounter2a() {
6371
}
6472

6573
async function encounter2b() {
66-
await inquirer.prompt({
74+
await inquirer.prompt<{ weapon: string }>({
6775
type: 'list',
6876
name: 'weapon',
6977
message: 'Pick one',

packages/inquirer/examples/input.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Input prompt example
33
*/
44

5-
import inquirer, { type DistinctQuestion } from 'inquirer';
5+
import inquirer from 'inquirer';
66

77
const answers = await inquirer.prompt([
88
{
@@ -45,6 +45,6 @@ const answers = await inquirer.prompt([
4545
return 'Please enter a valid phone number';
4646
},
4747
},
48-
] satisfies DistinctQuestion[]);
48+
]);
4949

5050
console.log(JSON.stringify(answers, null, ' '));

packages/inquirer/examples/list.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
* List prompt example
33
*/
44

5-
import inquirer, { type DistinctQuestion } from 'inquirer';
5+
import inquirer from 'inquirer';
66

7-
const answers = await inquirer.prompt([
7+
const answers = await inquirer.prompt<{ theme: string; size: string }>([
88
{
99
type: 'list',
1010
name: 'theme',
@@ -14,11 +14,7 @@ const answers = await inquirer.prompt([
1414
{ name: 'Make a reservation', value: 'reservation' },
1515
new inquirer.Separator(),
1616
{ name: 'Ask for opening hours', value: 'hours' },
17-
{
18-
name: 'Contact support',
19-
value: 'support',
20-
disabled: true,
21-
},
17+
{ name: 'Contact support', value: 'support', disabled: true },
2218
{ name: 'Talk to the receptionist', value: 'receptionist' },
2319
],
2420
},
@@ -38,6 +34,6 @@ const answers = await inquirer.prompt([
3834
return val.toLowerCase();
3935
},
4036
},
41-
] satisfies DistinctQuestion[]);
37+
]);
4238

4339
console.log(JSON.stringify(answers, null, ' '));

packages/inquirer/examples/long-list.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const multiLineChoices = [
2323
},
2424
];
2525

26-
const answers = await inquirer.prompt([
26+
const answers = await inquirer.prompt<{ letter: string; name: string[] }>([
2727
{
2828
type: 'list',
2929
loop: false,

packages/inquirer/examples/pizza.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,16 @@ import inquirer from 'inquirer';
77

88
console.log('Hi, welcome to Node Pizza');
99

10-
const answers = await inquirer.prompt([
10+
const answers = await inquirer.prompt<{
11+
toBeDelivered: boolean;
12+
phone: string;
13+
size: string;
14+
quantity: number;
15+
toppings: string;
16+
beverage: string;
17+
comments: string;
18+
prize?: string;
19+
}>([
1120
{
1221
type: 'confirm',
1322
name: 'toBeDelivered',
@@ -88,7 +97,7 @@ const answers = await inquirer.prompt([
8897
name: 'prize',
8998
message: 'For leaving a comment, you get a freebie',
9099
choices: ['cake', 'fries'],
91-
when(answers) {
100+
when(answers: { comments?: string }) {
92101
return answers.comments !== 'Nope, all good!';
93102
},
94103
},

packages/inquirer/examples/rawlist.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import inquirer from 'inquirer';
66

7-
const answers = await inquirer.prompt([
7+
const answers = await inquirer.prompt<{ theme: string; size: string }>([
88
{
99
type: 'rawlist',
1010
name: 'theme',

0 commit comments

Comments
 (0)