preact icon indicating copy to clipboard operation
preact copied to clipboard

The type of `this` is not `event.currentTarget` onChange events

Open esanzgar opened this issue 4 years ago • 2 comments
trafficstars

  • [ ] Check if updating to the latest Preact version resolves the issue

Describe the bug The type of this onChange events is not event.currentTarget as the type definition implies: https://github.com/preactjs/preact/blob/c7f57db13af43b8cc3353a138639ad9dd9523e16/src/jsx.d.ts#L368-L373

Also implied in this test: https://github.com/preactjs/preact/blob/c7f57db13af43b8cc3353a138639ad9dd9523e16/test/ts/Component-test.tsx#L5-L9

To Reproduce

https://codesandbox.io/embed/ancient-tree-6kj30?expanddevtools=1

Steps to reproduce the behavior:

  1. Click on the input checkbox
  2. See error in the console

Expected behavior The type of this should be the same as event.currentTarget

esanzgar avatar May 04 '21 12:05 esanzgar

I believe this was intentionally removed in order to prevent misleading this usage in event handlers that was also incompatible with React. Personally I would like to amend the TypeScript types to reflect this, and simply have the context of events be never or some value that implies it is not to be used.

developit avatar May 11 '21 20:05 developit

I see that now this has changed to type of never:

https://github.com/preactjs/preact/blob/2af05f533ca7a11afcf08d8914df261e2fbec52e/src/jsx.d.ts#L370-L376

Could you advise how can I create a wrapper around a native onClick prop? Before the above change, I used to do that like this: onClick?.call(this, event) but that throws an error now.

Same example in React:

https://www.staging-typescript.org/play?ts=4.6.0-dev.20220116&q=114#code/JYWwDg9gTgLgBAKjgQwM5wEoFNkGN4BmUEIcARFDvmQNwBQduEAdqvALICeAwgK5skAkszC94AXjgAKAN4tuAG2C4A1gBo4AOm2U2AXwBcmKjE0ARLDGTAFWACYAJACrsAMgAViYVAB5seU2FRGGc3AEEYGChgACMxLF9Q1yCxAFFbECxmGAA+DSSUmHSsTOycgEo4cRy4GTo4OCZWeBAefhgSRWUVKuksADcsmCNjAM12CH4sVMHsnwKRNIyhjQmpmaGKqpq6hoam1AhbTQUIAHMpAHJcJVV7S-L6Pbh5W5UAfk0pAaHH+rg9PR-pQYLwoMw4D5gIt4K9uuIZK0+AIQF1VHpatpNLoYHocj4APTQ4I5eiAoA

esanzgar avatar Jan 24 '22 15:01 esanzgar