-
Notifications
You must be signed in to change notification settings - Fork 90
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
[Request] Support non-string select / multiselect value
s
#44
Comments
Thanks for looking at this so quickly! Looking at your PR answers my original question a bit - seems like the primitive type restriction is due to On one hand I can get behind this, as non-primitives may not stringify cleanly. However, it does prevent valid use-cases of:
The second case in particular feels like enough of a reason to remove the primitive restriction. In the That all being said, that's just my opinion! Totally understand if you'd like to keep things as-is - happy to maintain my own fork / patch in projects. Thanks again! |
Why not explicitly provide that as IMO, more complex types could be handled in user land without much overhead. They ought to have a unique identifier to use as Finding the parent object from the returned value is trivial. |
That's fair - I'd be happy to provide my own const today = new Date();
const tomorrow = new Date(today);
tomorrow.setDate(tomorrow.getDate() + 1);
await select({
message: '',
options: [
{
label: `Today (${today.toLocaleDateString()})`,
value: today, // type error
},
{
label: `Tomorrow (${tomrorow.toLocaleDateString()})`,
value: tomorrow, // type error
}
],
}); Even though this behaves just fine at runtime and the date I could see this going either way:
|
Right, I see your point.
Like you've pointed out yourself it's not to please TypeScript per se, but rather the underlying implementation (i.e. allowing For the example at hand, doing a With that being said, with regards to your suggested solutions, I'm not in favor of # 1, but see no reason not to allow # 2, if technically possible (which I believe it is). I'll explore when I find the time. |
@chrissantamaria your example above is supported in the latest release. |
Really appreciate you taking a look at this! It does look like non-primitive types are allowed as import { select, isCancel } from '@clack/prompts';
const run = async () => {
const today = new Date();
const tomorrow = new Date(today);
tomorrow.setDate(tomorrow.getDate() + 1);
const value = await select({
message: '',
options: [
{
label: `Today (${today.toLocaleDateString()})`,
value: today,
},
{
label: `Tomorrow (${tomorrow.toLocaleDateString()})`,
value: tomorrow,
},
],
});
if (isCancel(value)) {
return;
}
console.log({ value }); // value inferred as unknown
};
run().catch((e) => {
console.error(e);
process.exit(1);
}); Haven't had time to dig into the internals yet, though I'm happy to do so! |
Sorry for not getting back to you. That's a real bummer... why, oh why, TypeScript? Can't you be a little decent and help out, if only just a little? I took a quick look at it but wasn't able to sort it out then. I'll try to get back to it ASAP. Maybe Not sure what's worse, if we should revert this until properly fixed or not. Thoughts @natemoo-re? |
I might be missing something, but just by reducing I imagine there's likely some reason this generic existed, though typechecking for the build still passes. If this feels like a reasonable solution I'm happy to put up a PR! |
Seems reasonable to me! Always good with fresh eyes... What say you @natemoo-re? Note: there are more components using |
Opened #102 with all |
Is your feature request related to a problem? Please describe.
The TypeScript types for
select
andmultiselect
options enforce thatvalue
fields passed extendReadonly<string>
:https://github.com/natemoo-re/clack/blob/d366a1f6e2339b5b97d0b9f42a8bdfd86333c2ba/packages/prompts/src/index.ts#L118-L120
This can be a bit cumbersome to use with non-string values, as it requires stringifying to pass to
select
+ parsing the result returned back.Describe the solution you'd like
Ideally (assuming no technical blockers + context I'm missing),
select
andmultiselect
could support any type of value. (perhaps naively) it looks like this could be as simple as changing the code block above to support a looserValue
generic.Describe alternatives you've considered
Stringifying + parsing as described above, though this isn't always feasible for more complex types.
Additional context
Interestingly, from my (relatively basic) testing, this only seems to be a type error. Passing in
Date
values works as expected - are there other types of values which may break this?If the fix is as simple as I'm imagining, I'd be happy to open a PR and take a stab at this! However, I'm still fairly early in my testing of this library so I may be missing some important context of why this limitation exists :)
The text was updated successfully, but these errors were encountered: