Skip to content

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

Closed
@aarongarciah

Description

@aarongarciah

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.

Metadata

Metadata

Assignees

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions