question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support custom events on native DOM elements

See original GitHub issue

Is your feature request related to a problem? Please describe. I get the following error when I’m trying to add custom events on native DOM elements:

Type '{ onswipestart: (event: CustomEvent<any>) => void; onswipemove: (event: CustomEvent<any>) => void; onswipeend: (event: CustomEvent<any>) => void; style: string; class: string; }' is not assignable to type 'HTMLProps<HTMLDivElement>'.
  Property 'onswipestart' does not exist on type 'HTMLProps<HTMLDivElement>'.ts(2322)

The custom events are dispatched to a div element using Sveltes actions/use directive https://svelte.dev/examples#actions).

Describe the solution you’d like Be able to type check for custom events dispatched using Svelte actions on native DOM elements.

Describe alternatives you’ve considered I could try to convert the div to an individual Svelte component but preferably it should work on native DOM elements as well.

Additional context

The error message

This is how I listen to the custom events:

<div
  class="bottomSheet"
  class:draggable
  bind:this={bottomSheet}
  use:swipeable
  on:swipestart={handleSwipeStart}
  on:swipemove={handleSwipeMove}
  on:swipeend={handleSwipeEnd}
  style="height:{height};bottom:{bottom};transform:translateY({$coords.ty}px);"
>

Related Pull Request for custom events on Svelte components: https://github.com/sveltejs/language-tools/pull/303

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:9
  • Comments:16 (10 by maintainers)

github_iconTop GitHub Comments

7reactions
jasonlyu123commented, Aug 6, 2020

Don’t know if there is a way to make it only applies to elements with the action. but you can make it globally available like this.

declare namespace svelte.JSX {
    interface HTMLAttributes<T> {
        onswipemove?: (event: CustomEvent<number> & { target: EventTarget & T }) => any;
    }
}
4reactions
johanbissemattssoncommented, Aug 6, 2020

Thanks, letting the user explicitly type the action or explicitly choose when to fall back to CustomEvent<any> would probably be the best case scenario but the solution by @jasonlyu123 is good enough for me now. Thanks for the great work!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Creating and triggering events - MDN Web Docs - Mozilla
This article demonstrates how to create and dispatch DOM events. Such events are commonly called synthetic events, as opposed to the events ......
Read more >
Custom events in JavaScript: A complete guide
In this tutorial, you can learn how to create custom events in JavaScript for your application to enhance the user experience.
Read more >
Dispatching custom events - The Modern JavaScript Tutorial
Custom events can be used to create “graphical components”. ... Built-in event classes form a hierarchy, similar to DOM element classes.
Read more >
Web Components API: Lifecycle Events and Custom Events
Native events are the main way we have to add interactivity on the web as we execute some code based on the user...
Read more >
A Complete Guide To Custom Events In JavaScript - Medium
Native DOM events are cancelable by default so we can call event.preventDeafult() on it which will prevent the default action of the event...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found