bigskysoftware / bigskysoftware/htmx
htmx.on has Typescript Event type instead of CustomEvent type on callback function
- Dominant language
- JavaScript
- Stars
- 49.4k
- Forks
- 1.7k
- Avg merge
- 3d 22h
- Merged PRs (30d)
- 30
Description
This issue is related to the Types definition for the event callback function for `htmx.on`. It's not a functional issue with the library code, it's a Typescript definition issue.
## Example
If you use the `htmx.on` function like this
```
htmx.on('htmx:beforeSwap', (event) => {
event.detail // this will emit a Typescript error because detail is not present on the Event type definition
});
```
The event variable should have the detail property and should be typed accordingly.
## About CustomEvent
I believe that the correct type of the callback function should be CustomEvent. This type is an extension of Event so this should not bring any braking changes. As I understood the CustomEvent is configurable with a dynamic type expressing the type for the detail property. If no generic is provided the property type is any. So first we should change the type to CustomEvent and then use/create a type to specify the detail property type.
Let me know if this makes sense and I can go ahead and I can make a PR.
## Workaround
An example of a workaround can be found here https://github.com/JacopoPatroclo/pht_stack/blob/main/client/main.ts and it's also a repo that reproduces this bug. (if you remove the workaround)
See related doc:
https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent
Contributor guide
Assessment
This issue has not been assessed yet.