Skip to content

Instantly share code, notes, and snippets.

@snewell92
Created April 3, 2023 20:20
Show Gist options
  • Save snewell92/a0406f3d58bb86d797e30f2034b1e996 to your computer and use it in GitHub Desktop.
Save snewell92/a0406f3d58bb86d797e30f2034b1e996 to your computer and use it in GitHub Desktop.
ChatGPT Prompt for overload issue
import cli, { InputQuestion, ListQuestion } from "inquirer";
interface TypeSafeInput<
TValidatedInput,
TName extends string
> extends InputQuestion<{ [name in TName]: TValidatedInput }> {
type: "input";
name: TName;
message: string;
typePredicate: (input: any) => input is TValidatedInput;
parse: (input: any) => TValidatedInput;
}
// We don't always need to do anything after the booleanp predicate is true,
// but other times we do need to parse the string to another type.
const identity = <T>(x: T) => x;
type SimpleObject<TKey extends string, TValue> = { [name in TKey]: TValue };
type SO<TK extends string, TV> = SimpleObject<TK, TV>;
// overloads of up to 4 inputs
export async function prompt<
T1, TN1 extends string,
T2, TN2 extends string,
T3, TN3 extends string,
T4, TN4 extends string
>(inputs: [
TypeSafeInput<T1, TN1>,
TypeSafeInput<T2, TN2>,
TypeSafeInput<T3, TN3>,
TypeSafeInput<T4, TN4>
]): Promise<SO<TN1, T1> & SO<TN2, T2> & SO<TN3, T3> & SO<TN4, T4>>;
export async function prompt<
T1, TN1 extends string,
T2, TN2 extends string,
T3, TN3 extends string,
>(inputs: [
TypeSafeInput<T1, TN1>,
TypeSafeInput<T2, TN2>,
TypeSafeInput<T3, TN3>
]): Promise<SO<TN1, T1> & SO<TN2, T2> & SO<TN3, T3>>;
export async function prompt<
T1, TN1 extends string,
T2, TN2 extends string,
>(inputs: [
TypeSafeInput<T1, TN1>,
TypeSafeInput<T2, TN2>
]): Promise<SO<TN1, T1> & SO<TN2, T2>>;
export async function prompt<T1, TN1 extends string>(inputs: [TypeSafeInput<T1, TN1>]): Promise<SO<TN1, T1>> {
const answers = await cli.prompt(inputs);
return inputs.reduce((accum, curr) => {
const parsedValue = curr.parse(answers[curr.name]);
accum[curr.name] = parsedValue;
return accum;
}, {} as SO<TN1, T1>);
}
@snewell92
Copy link
Author

It's suggestions to simplify do not, but the original suggestion did help me fix the code with this adjustment:

-export async function prompt<T1, TN1 extends string>(inputs: [TypeSafeInput<T1, TN1>]): Promise<SO<TN1, T1>> {
+export async function prompt<T1, TN1 extends string>(inputs: TypeSafeInput<T1, TN1>[]): Promise<SO<TN1, T1>> {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment