Skip to content

Commit 3e7a7de

Browse files
committed
fix: weaken internal group types to strengthen return/onCancel types
This changes a few things. First of all, trying to infer `T` while defining `T` currently doesn't work unless there's a member without the `results` arg. This is a known limitation that may or may not be fixed in typescript one day. Until that happens, if ever, this dumbs down the type of `results` to be a `Record<PropertyKey, unknown>`. This means _within a function_ you lose strong typing, but the return type will now always be correct. Secondly, `T` is actually the resulting already-awaited shape. This means we do not need `Awaited<T[K]>` since `T[K]` is already the awaited type. For example: ```ts type ActualType = { foo: number; bar: number; }; group<ActualType>({ foo: () => Promise.resolve(303), bar: () => Promise.resolve(808) }); ``` You can see the `ActualType` never needed `Awaited<T>` on each type since it is already the final result.
1 parent 5529c89 commit 3e7a7de

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

packages/prompts/src/index.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -844,16 +844,12 @@ export const spinner = ({ indicator = 'dots' }: SpinnerOptions = {}) => {
844844
};
845845
};
846846

847-
export type PromptGroupAwaitedReturn<T> = {
848-
[P in keyof T]: Exclude<Awaited<T[P]>, symbol>;
849-
};
850-
851847
export interface PromptGroupOptions<T> {
852848
/**
853849
* Control how the group can be canceled
854850
* if one of the prompts is canceled.
855851
*/
856-
onCancel?: (opts: { results: Prettify<Partial<PromptGroupAwaitedReturn<T>>> }) => void;
852+
onCancel?: (opts: { results: Prettify<Partial<T>> }) => void;
857853
}
858854

859855
type Prettify<T> = {
@@ -862,7 +858,7 @@ type Prettify<T> = {
862858

863859
export type PromptGroup<T> = {
864860
[P in keyof T]: (opts: {
865-
results: Prettify<Partial<PromptGroupAwaitedReturn<Omit<T, P>>>>;
861+
results: Record<PropertyKey, unknown>;
866862
}) => undefined | Promise<T[P] | undefined>;
867863
};
868864

@@ -873,7 +869,7 @@ export type PromptGroup<T> = {
873869
export const group = async <T>(
874870
prompts: PromptGroup<T>,
875871
opts?: PromptGroupOptions<T>
876-
): Promise<Prettify<PromptGroupAwaitedReturn<T>>> => {
872+
): Promise<Prettify<T>> => {
877873
const results = {} as any;
878874
const promptNames = Object.keys(prompts);
879875

0 commit comments

Comments
 (0)