Skip to content

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

@marekdedic

Description

@marekdedic

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions