Hacker0x01 / react-datepicker

A simple and reusable datepicker component for React
https://reactdatepicker.com/
MIT License
7.89k stars 2.23k forks source link

onChangeRaw differently typed in v7.0.0 #4901

Closed sm3sher closed 1 week ago

sm3sher commented 2 weeks ago

Describe the bug After updating to the newest types included version (7.0.0) onChangeRaw seems to be typed differently than before.

In @types/react-datepicker it was typed like this:

onChangeRaw: (event: React.FocusEvent<HTMLInputElement>) => void;

But in the latest version this has changed:

onChangeRaw: (event?: React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLElement>) => void;

The problem here is that neither React.MouseEvent nor React.KeyboardEvent provide the target as HTMLInputElement even though the target seems to be of type HTMLInputElement. This leads to event.target.value being invalid typescript.

My current workaround is to access the value via (event.target as HTMLInputElement).value but I would hope for a better solution.

To Reproduce

  1. Install the latest version 7.0.0
  2. Make use of onChangeRaw in a TS environment
  3. Try accessing event.target.value
  4. See TS error

Expected behavior onChangeRaw should provide the correct types or at least none that are in conflict with @types/react-datepicker.

Desktop (please complete the following information):

balajis-qb commented 2 weeks ago

I'll look into this issue

yuki0410-dev commented 1 week ago

@sm3sher (@balajis-qb @martijnrusschen ) Target is not necessarily HTMLInput, as onChangeRaw is also fired on Day Component selection and click events. For a typesafe implementation, why not use instanceof HTMLInputElement to determine event.target before accessing event.target.value?

sm3sher commented 1 week ago

You are indeed correct.

So far I've been filtering out the Day Component selection and click events by checking if (e.target.value !== undefined) to ensure I am working with an HTMLInputElement.

I will give it a try with instanceof check.

Update: Working with instanceof HTMLInputElement does the job for me.

I conclude from this that onChangeRaw is currently more precisely typed than it was in @types/react-datepicker?

yuki0410-dev commented 1 week ago

@sm3sher @balajis-qb ( cc. @martijnrusschen ) Are there any outstanding issues regarding this issue?

sm3sher commented 1 week ago

None that I know of. I am closing this issue.