Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ const name = await question("What's your name ?", {
### `select()`

```ts
select(message: string, options: SelectOptions): Promise<string>
select<T extends string>(message: string, options: SelectOptions<T>): Promise<T>
```

Scrollable select depending `maxVisible` (default `8`).
Expand All @@ -114,7 +114,7 @@ Use `options.skip` to skip prompt. It will return the first choice.
### `multiselect()`

```ts
multiselect(message: string, options: MultiselectOptions): Promise<[string]>
multiselect<T extends string>(message: string, options: MultiselectOptions<T>): Promise<T[]>
```

Scrollable multiselect depending `options.maxVisible` (default `8`).<br>
Expand Down Expand Up @@ -196,46 +196,46 @@ export interface AbstractPromptOptions {
stdin?: Stdin;
stdout?: Stdout;
message: string;
sginal?: AbortSignal;
skip?: boolean;
signal?: AbortSignal;
}

export interface PromptValidator<T = string | string[] | boolean> {
validate: (input: T) => boolean;
error: (input: T) => string;
export interface PromptValidator<T extends string | string[]> {
validate: (input: T) => boolean;
}

export interface QuestionOptions extends SharedOptions {
defaultValue?: string;
validators?: Validator[];
validators?: PromptValidator<string>[];
secure?: boolean;
}

export interface Choice {
value: any;
export interface Choice<T = any> {
value: T;
label: string;
description?: string;
}

export interface SelectOptions extends SharedOptions {
choices: (Choice | string)[];
export interface SelectOptions<T extends string> extends AbstractPromptOptions {
choices: (Choice<T> | T)[];
maxVisible?: number;
ignoreValues?: (string | number | boolean)[];
validators?: Validator[];
ignoreValues?: (T | number | boolean)[];
validators?: PromptValidator<string>[];
autocomplete?: boolean;
caseSensitive?: boolean;
}

export interface MultiselectOptions extends SharedOptions {
choices: (Choice | string)[];
export interface MultiselectOptions<T extends string> extends AbstractPromptOptions {
choices: (Choice<T> | T)[];
maxVisible?: number;
preSelectedChoices?: (Choice | string)[];
validators?: Validator[];
preSelectedChoices?: (Choice<T> | T)[];
validators?: PromptValidator<string[]>[];
autocomplete?: boolean;
caseSensitive?: boolean;
showHint?: boolean;
}

export interface ConfirmOptions extends SharedOptions {
export interface ConfirmOptions extends AbstractPromptOptions {
initial?: boolean;
}
```
Expand Down
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
"scripts": {
"build": "tsup index.ts --format cjs,esm --dts --clean",
"prepublishOnly": "npm run build",
"test": "glob -c \"tsx --test\" \"./test/**/*.test.ts\"",
"coverage": "c8 -r html npm run test",
"test-only": "glob -c \"tsx --test\" \"./test/**/*.test.ts\"",
"test-types": "npm run build && tsd",
"test": "c8 -r html npm run test-only && npm run test-types",
"lint": "eslint src test",
"lint:fix": "eslint . --fix"
},
Expand Down Expand Up @@ -36,6 +37,7 @@
"@types/node": "^24.0.3",
"c8": "^10.1.3",
"glob": "^11.0.0",
"tsd": "^0.33.0",
"tsup": "^8.3.5",
"tsx": "^4.19.2",
"typescript": "^5.7.2"
Expand All @@ -46,5 +48,8 @@
"bugs": {
"url": "https://github.com/TopCli/prompts/issues"
},
"homepage": "https://github.com/TopCli/prompts#readme"
"homepage": "https://github.com/TopCli/prompts#readme",
"tsd": {
"directory": "test/types"
}
}
2 changes: 1 addition & 1 deletion src/prompts/abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export interface AbstractPromptOptions {
signal?: AbortSignal;
}

export class AbstractPrompt<T> extends EventEmitter {
export class AbstractPrompt<T extends string | boolean> extends EventEmitter {
stdin: Stdin;
stdout: Stdout;
message: string;
Expand Down
12 changes: 8 additions & 4 deletions src/prompts/multiselect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const kRequiredChoiceProperties = ["label", "value"];
export interface MultiselectOptions<T extends string> extends AbstractPromptOptions {
choices: (Choice<T> | T)[];
maxVisible?: number;
preSelectedChoices?: (Choice | T)[];
validators?: PromptValidator<T[]>[];
preSelectedChoices?: (Choice<T> | T)[];
validators?: PromptValidator<string[]>[];
autocomplete?: boolean;
caseSensitive?: boolean;
showHint?: boolean;
Expand All @@ -27,7 +27,7 @@ type VoidFn = () => void;
export class MultiselectPrompt<T extends string> extends AbstractPrompt<T> {
#boundExitEvent: VoidFn = () => void 0;
#boundKeyPressEvent: VoidFn = () => void 0;
#validators: PromptValidator<T[]>[];
#validators: PromptValidator<string[]>[];
#showHint: boolean;

activeIndex = 0;
Expand All @@ -52,7 +52,11 @@ export class MultiselectPrompt<T extends string> extends AbstractPrompt<T> {
return this.choices.filter((choice) => this.#filterChoice(choice, autocompleteValue, isCaseSensitive));
}

#filterChoice(choice: T | Choice | string, autocompleteValue: string, isCaseSensitive = false) {
#filterChoice(
choice: T | Choice<T> | string,
autocompleteValue: string,
isCaseSensitive = false
) {
// eslint-disable-next-line no-nested-ternary
const choiceValue = typeof choice === "string" ?
(isCaseSensitive ? choice : choice.toLowerCase()) :
Expand Down
4 changes: 2 additions & 2 deletions src/prompts/question.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { isValid, type PromptValidator, resultError } from "../validators.js";

export interface QuestionOptions extends AbstractPromptOptions {
defaultValue?: string;
validators?: PromptValidator[];
validators?: PromptValidator<string>[];
secure?: boolean | {
placeholder: string;
};
Expand All @@ -22,7 +22,7 @@ export class QuestionPrompt extends AbstractPrompt<string> {
questionSuffixError: string;
answer?: string;
answerBuffer?: Promise<string>;
#validators: PromptValidator[];
#validators: PromptValidator<string>[];
#secure: boolean;
#securePlaceholder: string | null = null;

Expand Down
12 changes: 8 additions & 4 deletions src/prompts/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ export interface SelectOptions<T extends string> extends AbstractPromptOptions {
choices: (Choice<T> | T)[];
maxVisible?: number;
ignoreValues?: (T | number | boolean)[];
validators?: PromptValidator<T>[];
validators?: PromptValidator<string>[];
autocomplete?: boolean;
caseSensitive?: boolean;
}

type VoidFn = () => void;

export class SelectPrompt<T extends string = string> extends AbstractPrompt<T> {
export class SelectPrompt<T extends string> extends AbstractPrompt<T> {
#boundExitEvent: VoidFn = () => void 0;
#boundKeyPressEvent: VoidFn = () => void 0;
#validators: PromptValidator<T>[];
#validators: PromptValidator<string>[];
activeIndex = 0;
questionMessage: string;
autocompleteValue = "";
Expand All @@ -48,7 +48,11 @@ export class SelectPrompt<T extends string = string> extends AbstractPrompt<T> {
return this.choices.filter((choice) => this.#filterChoice(choice, autocompleteValue, isCaseSensitive));
}

#filterChoice(choice: Choice | string, autocompleteValue: string, isCaseSensitive = false) {
#filterChoice(
choice: Choice<T> | string,
autocompleteValue: string,
isCaseSensitive = false
) {
// eslint-disable-next-line no-nested-ternary
const choiceValue = typeof choice === "string" ?
(isCaseSensitive ? choice : choice.toLowerCase()) :
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface Choice<T = any> {
export interface Choice<T extends string> {
value: T;
label: string;
description?: string;
Expand Down
4 changes: 2 additions & 2 deletions src/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ export type ValidationResponse = InvalidResponse | ValidResponse;
export type InvalidResponse = string | InvalidResponseObject;
export type ValidResponse = null | undefined | true | ValidResponseObject;

export interface PromptValidator<T = string | string[] | boolean> {
export interface PromptValidator<T extends string | string[]> {
validate: (input: T) => ValidationResponse;
}

export function required<T = string | string[] | boolean>(): PromptValidator<T> {
export function required(): PromptValidator<any> {
return {
validate: (input) => {
const isValid = (Array.isArray(input) ? input.length > 0 : Boolean(input));
Expand Down
57 changes: 57 additions & 0 deletions test/types/api.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Import Third-party Dependencies
import { expectType } from "tsd";

// Import Internal Dependencies
import {
question,
confirm,
select,
multiselect,
type PromptValidator
} from "../../index.js";

const stringNotEmptyValidator: PromptValidator<string> = {
validate(input) {
return input.trim().length === 0 ?
{ isValid: false, error: "Input was empty" } :
{ isValid: true };
}
};

expectType<Promise<string>>(
question("message", {
validators: [stringNotEmptyValidator]
})
);

expectType<Promise<"A" | "B">>(
select("message", { choices: ["A", "B"] })
);
expectType<Promise<"A" | "B">>(
select("message", {
choices: [
{ value: "A", label: "Option A" },
{ value: "B", label: "Option B" }
]
})
);

expectType<Promise<("A" | "B")[]>>(
multiselect("message", { choices: ["A", "B"] })
);
expectType<Promise<("A" | "B")[]>>(
multiselect("message", {
choices: [
{ value: "A", label: "Option A" },
{ value: "B", label: "Option B" }
],
preSelectedChoices: ["A"]
})
);

expectType<Promise<boolean>>(
confirm("message")
);
expectType<Promise<boolean>>(
confirm("message", { initial: true })
);
Loading