Skip to content

New rule: require-event-dispatcher-types #346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
marekdedic opened this issue Jan 22, 2023 · 5 comments · Fixed by #354
Closed

New rule: require-event-dispatcher-types #346

marekdedic opened this issue Jan 22, 2023 · 5 comments · Fixed by #354
Labels
enhancement New feature or request new rule

Comments

@marekdedic
Copy link
Contributor

Motivation

Hi,
when using svelte with TypeScript, it is possible to add types to events by typing the createEventDispatcher function so:

import { createEventDispatcher } from "svelte";

const dispatch = createEventDispatcher<{one: never, two: number}>();

dispatch("one"); // OK
dispatch("two", 3); // OK
dispatch("one", "Hello world"); // Fails
dispatch("two"); // Fails
dispatch("two", "Hello world"); // Fails

I propose a rule that would require all calls to createEventDispatcher to include the templated type.

Description

The rule would trigger on any call of createEventDispatcher witchou the templated type

Examples

import { createEventDispatcher } from "svelte";

<!-- ✓ GOOD -->
const dispatch = createEventDispatcher<{one: never, two: number}>();

const dispatch = createEventDispatcher<any>();

const dispatch = createEventDispatcher<{}>();

<!-- ✗ BAD -->
const dispatch = createEventDispatcher();

Additional comments

No response

@marekdedic marekdedic added enhancement New feature or request new rule labels Jan 22, 2023
@marekdedic
Copy link
Contributor Author

Oh and if you type your events, all the listeners get typechecked as well, so this really is something you want to do if you are using TS...

@ota-meshi
Copy link
Member

Thanks for the rule suggestions.
That rule sounds almost good to me.

But why is the example below marked as GOOD? I think they are pretty much the same if you omit the type.

const dispatch = createEventDispatcher<any>();

const dispatch = createEventDispatcher<{}>();

@marekdedic
Copy link
Contributor Author

Hmm, originally, I though of the rule as being a reminder not to forget the types - that's why I put those examples there, I didn't really care about what the type is, just that there is one...

I think any doesn't really make sense - it is the same as if you omit the type - so putting any in there or manually suppressing the rule does the same thing.

As with the {} type - I meant Record<string, never>, an empty object (this part of TS is a bit confusing...). But that would make it so that no events can be dispatched, so the whole call is useless and doesn't make any sense...

The rule could report for both of those - I think it probably should report for any, with the empty object I wouldn't bother - that's a silly example not worth implementing a check for...

@ota-meshi
Copy link
Member

Thank you for your opinion.

I though of the rule as being a reminder not to forget the types I though of the rule as being a reminder not to forget the types

I hear you and I agree with it. The any and {} may be intentional for the user.
I think it makes sense not to check them to avoid complicating the implementation of the rule.
If there is user feedback that they want to check strictly, we can add the option later.

@marekdedic
Copy link
Contributor Author

Great, yeah, let's make it simple if there's no need to make the rule more complex...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request new rule
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants