Skip to content

New rule: require-strict-events #345

Closed
@marekdedic

Description

@marekdedic

Motivation

Hi,
when using TypeScript with svelte, component events aren't automatically typed - e. g. you can have an on:nonexistent={() => {}} handle for a non-existent event. However, svelte does check this, if the opening script tag of the child component has the strictEvents attribute. (<script lang="ts" strictEvents>).

I propose to add a rule that would enforce this.

Description

The rule should enforce that components have the strictEvents attribute present in their opening svelte tag.

I propose several an option to choose for which components the attribute should be enforced:

  1. Require the attribute on all components. This makes sense since for a component that does not dispatch any event, the attribute still makes svelte check that no event listeners are present. I propose that this should be the default.
  2. Only check components that actually dispatch an event - this would probably be more complicated to implement

Additional note: For some components that have event mixins, the strictEvents attribute does not work and you have to declare the $$Events interface. In that case, I think the rule shouldn't fire - so effectively, it would require either the strictEvents attribute or a manually declared interface $$Events for the component.

Examples

<!-- ✓ GOOD -->
<script lang="ts" strictEvents>
</script>

<script lang="ts">
  interface $$Events {}
</script>

<!-- ✗ BAD -->
<script lang="ts">
</script>

Additional comments

The relevant RFC.

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