You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The types for DatePicker and DateRangePicker are created with ValueBase<T | null, MappedDateValue<T>> (or ValueBase<RangeValue<T>, RangeValue<MappedDateValue<T>>>), where T extends DateValue, which means the onChange handler is typed as onChange?: (value: MappedDateValue<T>) => void. The MappedDateValuetype is non-nullable, but it's trivial to cause these components to call onChange with a null. The type of the onChange parameter should be nullable.
🤔 Expected Behavior?
The exported type of onChange for DatePicker is (value: MappedDateValue<T> | null) => void and for DateRangePicker (value: RangeValue<MappedDateValue<T>> | null) => void.
😯 Current Behavior
The onChange parameter isn't typed as nullable, so TypeScript fails to alert users to a possible TypeError exception (eg due to de-structuring null).
💁 Possible Solution
Update the types for DatePicker, DateRangePicker, etc. to include null for the second generic parameter to ValueBase. For example:
While creating a custom component composing DateRangePicker, I created an onChange handler similar to the following:
<DateRangePickeronChange={({ start, end })=> ...}{...others}/>
I was surprised to get no error from TypeScript about the argument potentially being null. Here, a TypeError is thrown at runtime when trying to destructure null.
Provide a general summary of the issue here
The types for DatePicker and DateRangePicker are created with
ValueBase<T | null, MappedDateValue<T>>
(orValueBase<RangeValue<T>, RangeValue<MappedDateValue<T>>>
), whereT extends DateValue
, which means theonChange
handler is typed asonChange?: (value: MappedDateValue<T>) => void
. TheMappedDateValue
type is non-nullable, but it's trivial to cause these components to callonChange
with anull
. The type of theonChange
parameter should be nullable.🤔 Expected Behavior?
The exported type of
onChange
for DatePicker is(value: MappedDateValue<T> | null) => void
and for DateRangePicker(value: RangeValue<MappedDateValue<T>> | null) => void
.😯 Current Behavior
The
onChange
parameter isn't typed as nullable, so TypeScript fails to alert users to a possible TypeError exception (eg due to de-structuringnull
).💁 Possible Solution
Update the types for DatePicker, DateRangePicker, etc. to include
null
for the second generic parameter toValueBase
. For example:What's the use case for MappedDateValue? I haven't found a reason why the type can't be simplified to just:
🔦 Context
While creating a custom component composing DateRangePicker, I created an onChange handler similar to the following:
I was surprised to get no error from TypeScript about the argument potentially being
null
. Here, a TypeError is thrown at runtime when trying to destructurenull
.🖥️ Steps to Reproduce
Sandbox: https://codesandbox.io/p/sandbox/long-surf-m5zd6x
Screen.Recording.2024-05-06.at.9.26.53.PM.mov
I imagine the "Clear" button example in the documentation will also cause
onChange
to be called withnull
.Version
1.2.0
What browsers are you seeing the problem on?
Firefox, Chrome, Safari
If other, please specify.
No response
What operating system are you using?
mac OS 14.3
🧢 Your Company/Team
No response
🕷 Tracking Issue
No response
The text was updated successfully, but these errors were encountered: