Merge pull request #30243 from danielpa9708/danielpa9708-patch-1

[prompts] infer answers types
This commit is contained in:
Armando Aguirre
2018-11-08 13:28:45 -08:00
committed by GitHub
2 changed files with 36 additions and 17 deletions

View File

@@ -1,12 +1,16 @@
// Type definitions for prompts 1.1
// Project: https://github.com/terkelg/prompts
// Definitions by: Berkay GURSOY <https://github.com/Berkays>
// Daniel Perez Alvarez <https://github.com/danielpa9708>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.1
// TypeScript Version: 2.8
export = prompts;
declare function prompts(questions: prompts.PromptObject | prompts.PromptObject[], options?: prompts.Options): any;
declare function prompts<T extends string = string>(
questions: prompts.PromptObject<T> | Array<prompts.PromptObject<T>>,
options?: prompts.Options
): Promise<prompts.Answers<T>>;
declare namespace prompts {
// Circular reference from prompts
@@ -15,8 +19,7 @@ declare namespace prompts {
function inject(obj: any): void;
namespace inject {
const prototype: {
};
const prototype: {};
}
namespace prompts {
@@ -51,15 +54,15 @@ declare namespace prompts {
onCancel: (prompt: PromptObject, answers: any) => void;
}
interface PromptObject {
type: string | ((prev: any, values: any, prompt: PromptObject) => void);
name: string | ((prev: any, values: any, prompt: PromptObject) => void);
message?: string | ((prev: any, values: any, prompt: PromptObject) => void);
interface PromptObject<T extends string = string> {
type: ValueOrFunc<string>;
name: ValueOrFunc<T>;
message?: ValueOrFunc<string>;
initial?: string;
style?: string;
format?: ((prev: any, values: any, prompt: PromptObject) => void);
validate?: ((prev: any, values: any, prompt: PromptObject) => void);
onState?: ((prev: any, values: any, prompt: PromptObject) => void);
format?: PrevCaller<T, void>;
validate?: PrevCaller<T, void>;
onState?: PrevCaller<T, void>;
min?: number;
max?: number;
float?: boolean;
@@ -73,4 +76,14 @@ declare namespace prompts {
suggest?: ((prev: any, values: any, prompt: PromptObject) => void);
limit?: number;
}
type Answers<T extends string> = { [id in T]: any };
type PrevCaller<T extends string, R = T> = (
prev: any,
values: Answers<T>,
prompt: PromptObject
) => R;
type ValueOrFunc<T extends string> = T | PrevCaller<T>;
}

View File

@@ -1,8 +1,14 @@
import prompts = require("prompts");
const response = prompts({
type: 'number',
name: 'value',
message: 'Input value to double:',
validate: (value: any) => value < 0 ? `Cant be less than zero` : true
});
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;
})();