if multiple is false, the return type is T, and if multiple is true, the return type is T[]
export async function askListQuestion<T extends string, U extends boolean>(
question: string,
options: T[],
multiple: U,
): Promise<U extends true ? T[] : T> {
return (
await inquirer.prompt<{ answer: U extends true ? T[] : T }>({
name: 'answer',
message: question,
type: multiple ? 'checkbox' : 'list',
choices: options as string[],
})
).answer
}
but, i cant do multiple: U = false, for some reason, i get this error
Type 'boolean' is not assignable to type 'U'.
'boolean' is assignable to the constraint of type 'U', but 'U' could be instantiated with a different subtype of constraint 'boolean'.
and if i do multiple?: U, then it works if i explicitly pass true or false, but if i dont send anything, it says its T | T[], when i expected it to be T (because undefined doesnt extends true, right?)
so, any idea how to make it treat an undefined as a false?