DefinitelyTyped/types/prompts/prompts-tests.ts
Colin 29a267c55b [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
2019-05-20 10:23:55 -07:00

35 lines
1.1 KiB
TypeScript

import prompts = require("prompts");
type HasProperty<T, K> = K extends keyof T ? true : false;
(async () => {
const response = await prompts({
type: "number",
name: "value",
message: "Input value to double:",
validate: (value: any) => (value < 0 ? `Cant be less than zero` : true)
});
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?"
}
]);
})();