How to get "Choice" type?
I need to tell a select prompt that my array for the choices option is an array of Choices, but its not working.
I need this type
type Choice<Value> = {
value: Value;
name?: string;
description?: string;
short?: string;
disabled?: boolean | string;
type?: never;
};
But using Choice<Value>[] isn't valid, as Value isn't exported? If I just use Choice[] then all the properties are required
I think you'll loose type inference and the generic if you type it manually. I wouldn't recommend typing the choices array manually. Do you run into issues if you do? (code example could help too)
I'm not wanting to type it manually, I want to use the types defined in the library.
eg I have this code
const users = await cognito.getUsers(userPoolID as string);
const username = await select({
message: 'User',
choices: users,
});
const users should be an array of type Choice<Value> ie Choice<Value>[] but thats not valid.
Can you post the typescript error you get?
First, trying to use Choice[], then trying to use Choice<Value>[]
Could it be helpful to export the Choice type so that devs have access to it, similar to how we can do:
import type { Separator } from "@inquirer/prompts";
@medallyon I don't really want to do that because then we tie type interfaces to our public APIs and to breaking changes. I don't believe the problem Paul is running into here is linked to the lack of exported Choice.
@paul-uz Can you remove the casting completely. I want to understand why the types aren't inferred properly - you shouldn't need to type it manually. I think you have more of an issue with the return type of cognito.getUsers than with inquirer - but with the types you added, we're burying this problem.
So I managed to get closer, ensuring I made my function return a type of (Separator | Choice<any>)[] but now get the error of Type '(Separator | Choice<any>)[]' is not assignable to type 'readonly (Separator | Choice<any>)[]'. so I guess I'm now blocked unless the types in the library are changed.
@paul-uz can you send a minimal reproduction case? I'd be very happy to help, but it's really hard for me to provide help and direction without access to the exact code, and without being able to reproduce your issue.
import { type Separator, select } from "@inquirer/prompts";
type Choice<T> = Exclude<
Parameters<typeof select<T>>[0]["choices"][number],
string | Separator
>;
@SBoudrias
What is the problem with exporting types that lib uses?
I'm trying to build a function that maps an array of strings into a structured choices array, with formatted labels and optional group separators. However, I can’t safely create my own types because I don’t have visibility into all the fields the library expects.
Without access to the internal types, there's a high risk of falling out of sync with the library’s API, such as missing new fields, structural changes, or deprecations in future releases. This would require manually inspecting updates and maintaining custom types, which is unreliable and burdensome.
At least, add @alex-kinokon solution to your docs.
Like I explained earlier, I don't want to tie the public API of the library to types definitions. And for most users, manually typings is a bad idea as it break proper type inference.
If you're passing in const, inquirer can define exact returns types. Passing in types that aren't const, you loose this capacity and get out a type that's all possible return type - even though typescript would otherwise know those values cannot be returned.
Happy to document this workaround in the Recipe section of the README.