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.

Discussion: How can we type the events supported by a component

See original GitHub issue

The goal is to easily catch typos and help the discovery of all possible events dispatched by a component. After all, there is no reason why the inputs (props) can have a strong contract but the outputs (events) don’t. In fact, it’s even more important than for props, because the dispatching can occur about anywhere in the file, whereas props are usually neatly bunched together are the top.

Right now I can type on:qoidfoqidjoiqsjd just fine.

Flex, which also had its compiler used custom annotations for this: https://github.com/apache/flex-sdk/blob/master/frameworks/projects/mx/src/mx/core/Container.as#L97

Perhaps we could have a special, reserved convention like export type Events = {name: 'eventA', data: number} | ... Having the TSDoc carry over on the consumer side tooltip would be the icing on the cake.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:9
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

9reactions
dummdidummcommented, Sep 18, 2020

You can now take advantage of the createEventDispatcher typing introduced in Svelte 3.25.0. If you do

const dispatch = createEventDispatcher<{foo: string; bar: boolean}>();

You get strong typing for dispatch within the component, and if you listen to the foo/bar events, you’ll get strong typings:

on:foo={e => ... // <- e is of type CustomEvent<string>

You’ll also get much better autocompletion for events now. Note however that you are still allowed to listen to other events, so type safety in the sense of “only listen to events defined through createEventDispatcher” is still only possible through the ComponentEvents interface. But we plan on providing something (maybe a script tag attribute) which would make the events strict.

Related docs: https://github.com/sveltejs/language-tools/blob/master/docs/preprocessors/typescript.md#typing-component-events

2reactions
dummdidummcommented, Aug 5, 2020

The reason why this cannot be implemented easily is that it’s a lot harder to collect all possible events than to collect the props. That’s also the reason why autocompletion does not work because these are for props.

Looking under the hood it’s that props are implemented through jsx props. Events are not because of the limitations of collecting them. If the user would explicitly type them this could be changed. We cannot mark the dispatched events as “this does not conform to the definition” though because of the challenges mentioned.

Having a reserved interface is likely the way forward. We have thought about this before in the context of generic props. Reserved interface names could be ComponentEvents, ComponentProps, ComponentSlots and ComponentDef for typing all three.

Related #304

Read more comments on GitHub >

github_iconTop Results From Across the Web

Introduction to events - Learn web development | MDN
To react to an event, you attach an event handler to it. This is a block of code (usually a JavaScript function that...
Read more >
What is an event handler and how does it work? - TechTarget
In programming, an event handler is a callback routine that operates asynchronously once an event takes place. It dictates the action that follows...
Read more >
SyntheticEvent - React
The event handlers below are triggered by an event in the bubbling phase. To register an event handler for the capture phase, append...
Read more >
AWT Event Handling - Tutorialspoint
Events are generated as result of user interaction with the graphical user interface components. For example, clicking on a button, moving the mouse,...
Read more >
Talk to your React components with custom events
You can create a simple synthetic event with a custom type using the Event constructor: const event = new Event('build'); document.
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