Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(@clack/core,@clack/prompts): add non empty opts type interface #145

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export { default as SelectPrompt } from './prompts/select';
export { default as SelectKeyPrompt } from './prompts/select-key';
export { default as TextPrompt } from './prompts/text';
export { block } from './utils';
export type { NonEmptyArray } from './utility-types';
5 changes: 3 additions & 2 deletions packages/core/src/prompts/group-multiselect.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { NonEmptyArray } from '../utility-types';
import Prompt, { PromptOptions } from './prompt';

interface GroupMultiSelectOptions<T extends { value: any }>
extends PromptOptions<GroupMultiSelectPrompt<T>> {
options: Record<string, T[]>;
options: Record<string, NonEmptyArray<T>>;
initialValues?: T['value'][];
required?: boolean;
cursorAt?: T['value'];
}
export default class GroupMultiSelectPrompt<T extends { value: any }> extends Prompt {
options: (T & { group: string | boolean })[];
options: NonEmptyArray<T & { group: string | boolean }>;
cursor: number = 0;

getGroupItems(group: string): T[] {
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/prompts/multi-select.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { NonEmptyArray } from '../utility-types';
import Prompt, { PromptOptions } from './prompt';

interface MultiSelectOptions<T extends { value: any }> extends PromptOptions<MultiSelectPrompt<T>> {
options: T[];
options: MultiSelectPrompt<T>['options'];
initialValues?: T['value'][];
required?: boolean;
cursorAt?: T['value'];
}
export default class MultiSelectPrompt<T extends { value: any }> extends Prompt {
options: T[];
options: NonEmptyArray<T>;
cursor: number = 0;

private get _value() {
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/prompts/select.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { NonEmptyArray } from '../utility-types';
import Prompt, { PromptOptions } from './prompt';

interface SelectOptions<T extends { value: any }> extends PromptOptions<SelectPrompt<T>> {
options: T[];
options: SelectPrompt<T>['options'];
initialValue?: T['value'];
}
export default class SelectPrompt<T extends { value: any }> extends Prompt {
options: T[];
options: NonEmptyArray<T>;
cursor: number = 0;

private get _value() {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/utility-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type NonEmptyArray<T> = [T, ...T[]];
7 changes: 4 additions & 3 deletions packages/prompts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
GroupMultiSelectPrompt,
isCancel,
MultiSelectPrompt,
NonEmptyArray,
PasswordPrompt,
SelectKeyPrompt,
SelectPrompt,
Expand Down Expand Up @@ -176,7 +177,7 @@ type Option<Value> = Value extends Primitive

export interface SelectOptions<Value> {
message: string;
options: Option<Value>[];
options: NonEmptyArray<Option<Value>>;
initialValue?: Value;
maxItems?: number;
}
Expand Down Expand Up @@ -299,7 +300,7 @@ export const selectKey = <Value extends string>(opts: SelectOptions<Value>) => {

export interface MultiSelectOptions<Value> {
message: string;
options: Option<Value>[];
options: NonEmptyArray<Option<Value>>;
initialValues?: Value[];
required?: boolean;
cursorAt?: Value;
Expand Down Expand Up @@ -415,7 +416,7 @@ export const multiselect = <Value>(opts: MultiSelectOptions<Value>) => {

export interface GroupMultiSelectOptions<Value> {
message: string;
options: Record<string, Option<Value>[]>;
options: Record<string, NonEmptyArray<Option<Value>>>;
initialValues?: Value[];
required?: boolean;
cursorAt?: Value;
Expand Down