Skip to content
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

Event handlers use incorrect assumption about event.target #579

Closed
scottgonzalez opened this issue Sep 26, 2020 · 3 comments
Closed

Event handlers use incorrect assumption about event.target #579

scottgonzalez opened this issue Sep 26, 2020 · 3 comments
Labels
bug Something isn't working Fixed Fixed in master branch. Pending production release.

Comments

@scottgonzalez
Copy link

Describe the bug
The following event listener will assume that event.target will be of type HTMLDivElement:

<div on:input={handleInputChange}>
    ...
</div>

However, the target of any bubbling event cannot be assumed. Trying to cast target to the appropriate type still causes a TypeScript error because HTMLDivElement and HTMLInputElement are not compatible.

    function handleInputChange(event: InputEvent): void {
        const target = event.target as HTMLInputElement;
        ...
    }

Expected behavior
event.target can be used properly in TypeScript.

Information about your Svelte project:

  • Svelte version: 3.26.0

Severity
Annoying as it prevents automated TypeScript checking from being part of our workflow.

@Conduitry Conduitry transferred this issue from sveltejs/svelte Sep 26, 2020
@dummdidumm dummdidumm added the bug Something isn't working label Sep 26, 2020
@dummdidumm
Copy link
Member

As a workaround you can cast to unknown or any first.

@scottgonzalez
Copy link
Author

Thanks. That at least gives us a path to get type checking to work.

It also looks like the generic Event type is always used instead of the specific event type, i.e., InputEvent.

dummdidumm added a commit that referenced this issue Sep 29, 2020
The target of bubbling events cannot be assumed. Use `currentTarget` instead.
#579
@dummdidumm dummdidumm added the Fixed Fixed in master branch. Pending production release. label Sep 29, 2020
@dummdidumm
Copy link
Member

target is fixed. About the event types: the more specific ones according to the MDN/TS typings are used; they don't use InputEvent.

maxlath added a commit to inventaire/inventaire-client that referenced this issue Apr 15, 2024
…arget

as the later's type is undeterimate.
See https://stackoverflow.com/questions/70031167/event-type-typescript-for-event-handler-in-svelte#answer-70032127
and sveltejs/language-tools#579

I initially used the following generic type

  type EventWithCurrentTarget<Event, Target = HTMLInputElement> = Event & { currentTarget: Target }

but it's actually not needed so far
maxlath added a commit to inventaire/inventaire-client that referenced this issue Apr 16, 2024
…arget

as the later's type is undeterimate.
See https://stackoverflow.com/questions/70031167/event-type-typescript-for-event-handler-in-svelte#answer-70032127
and sveltejs/language-tools#579

I initially used the following generic type

  type EventWithCurrentTarget<Event, Target = HTMLInputElement> = Event & { currentTarget: Target }

but it's actually not needed so far
maxlath added a commit to inventaire/inventaire-client that referenced this issue Apr 16, 2024
…arget

as the later's type is undeterimate.
See https://stackoverflow.com/questions/70031167/event-type-typescript-for-event-handler-in-svelte#answer-70032127
and sveltejs/language-tools#579

I initially used the following generic type

  type EventWithCurrentTarget<Event, Target = HTMLInputElement> = Event & { currentTarget: Target }

but it's actually not needed so far
maxlath added a commit to inventaire/inventaire-client that referenced this issue Apr 19, 2024
…arget

as the later's type is undeterimate.
See https://stackoverflow.com/questions/70031167/event-type-typescript-for-event-handler-in-svelte#answer-70032127
and sveltejs/language-tools#579

I initially used the following generic type

  type EventWithCurrentTarget<Event, Target = HTMLInputElement> = Event & { currentTarget: Target }

but it's actually not needed so far
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Fixed Fixed in master branch. Pending production release.
Projects
None yet
Development

No branches or pull requests

2 participants