[prompts] Fix type and validate function definitions (#35510)

* Allow `type` function to return `Falsy`

As defined in the documentation here: https://github.com/terkelg/prompts#type

The `type` field can be a function which returns a `Falsy` value to disable the prompt, which is currently not allowed by these type definitions:

> If `type` is a falsy value the prompter will skip that question.

* fix validate return type
This commit is contained in:
Colin
2019-05-20 10:23:55 -07:00
committed by Ryan Cavanaugh
parent 4ab6d0665f
commit 29a267c55b
2 changed files with 23 additions and 3 deletions
+3 -3
View File
@@ -1,4 +1,4 @@
// Type definitions for prompts 2.04
// Type definitions for prompts 2.0
// Project: https://github.com/terkelg/prompts
// Definitions by: Berkay GURSOY <https://github.com/Berkays>
// Daniel Perez Alvarez <https://github.com/danielpa9708>
@@ -60,13 +60,13 @@ declare namespace prompts {
}
interface PromptObject<T extends string = string> {
type: ValueOrFunc<PromptType> | Falsy;
type: PromptType | Falsy | PrevCaller<T, PromptType | Falsy>;
name: ValueOrFunc<T>;
message?: ValueOrFunc<string>;
initial?: string | number | boolean | Date;
style?: string;
format?: PrevCaller<T, void>;
validate?: PrevCaller<T, void>;
validate?: PrevCaller<T, boolean | string>;
onState?: PrevCaller<T, void>;
min?: number;
max?: number;
+20
View File
@@ -12,3 +12,23 @@ type HasProperty<T, K> = K extends keyof T ? true : false;
const HasPropValue: HasProperty<typeof response, "value"> = true;
const DoesntHavePropAsdf: HasProperty<typeof response, "asdf"> = false;
})();
(async () => {
await prompts([
{
type: 'text',
name: 'language',
message: "What langauge is the next greatest thing since sliced bread?",
},
{
type: (prev, values) => {
const HasPromptName: HasProperty<typeof values, "language"> = true;
const DoesntHavePromptTypes: HasProperty<typeof values, "text"> = false;
return prev === 'javascript' ? 'confirm' : null;
},
name: 'confirmation',
message: "Have you tried TypeScript?"
}
]);
})();