Skip to content
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

[Slider] value type should narrow onValueChange value type #1230

Open
aarongarciah opened this issue Dec 25, 2024 · 0 comments · May be fixed by #1241
Open

[Slider] value type should narrow onValueChange value type #1230

aarongarciah opened this issue Dec 25, 2024 · 0 comments · May be fixed by #1241
Labels
component: slider This is the name of the generic UI component, not the React module! enhancement This is not a bug, nor a new feature typescript

Comments

@aarongarciah
Copy link
Member

aarongarciah commented Dec 25, 2024

Feature request

Summary

The Slider accepts values of type Array<number> | number but the onValueChange and onValueCommitted callbacks don't narrow the type of the value parameter to number or Array<number> and users need to type cast it manually.

Example of code throwing a TS error:

//  ❌ Argument of type 'number | number[]' is not assignable to parameter of type 'SetStateAction<number>'.
//    Type 'number[]' is not assignable to type 'SetStateAction<number>'.

const [value, setValue] = React.useState(25);
<Slider.Root value={value} onValueChange={(value) => setValue(value}>

Example of type casting needed to overcome the TS error:

const [value, setValue] = React.useState(25);
<Slider.Root value={value} onValueChange={(value) => setValue(value as number}>

Motivation

Avoid users needing to do manual type casting.

@aarongarciah aarongarciah added component: slider This is the name of the generic UI component, not the React module! enhancement This is not a bug, nor a new feature labels Dec 25, 2024
@seloner seloner linked a pull request Dec 28, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: slider This is the name of the generic UI component, not the React module! enhancement This is not a bug, nor a new feature typescript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants