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

Feature: Add Generics to Event target #49504

Closed
5 tasks done
TheColorRed opened this issue Jun 12, 2022 · 2 comments
Closed
5 tasks done

Feature: Add Generics to Event target #49504

TheColorRed opened this issue Jun 12, 2022 · 2 comments
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript

Comments

@TheColorRed
Copy link

Suggestion

πŸ” Search Terms

  • Event target generics

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

This would add the ability to use generics to type the target of an event.

πŸ“ƒ Motivating Example

This would remove the requirement of having to cast it each time you use it or assigning the target to a variable.

function myEvent(evt: KeyboardEvent) {
  // Option "A" save to variable and cast (Still good for large blocks of code)
  const input = evt.target as HTMLInputElement

  // Option "B" cast target every time
  if ((evt.target as HTMLInputElement).value === 'a') {
    // Do something
  } else if ((evt.target as HTMLInputElement).value === 'b') {
    // Do something else
  }
}

πŸ’» Use Cases

With a generic type, you get a cleaner more readable result:

function myEvent(evt: KeyboardEvent<HTMLInputElement>) {
  if (evt.target.value === 'a') {
    // Do something
  } else if (evt.target.value === 'b') {
    // Do something else
  }
}
function myEvent(evt: KeyboardEvent<HTMLInputElement | HTMLDivElement>) {
  if (evt.target instanceof HTMLInputElement && evt.target.value === 'a') {
    // Do something
  } else if (evt.target instanceof HTMLDivElement && evt.target.innerText === 'b') {
    // Do something else
  }
}
@MartinJohns
Copy link
Contributor

MartinJohns commented Jun 12, 2022

The target doesn't has to be the element you attached your event handler onto. TypeScript can't ensure that the generic type argument matches, and generated runtime checks are a no-go.

So the only "benefit" is that you don't have to use the type assertion, which is actually a huge drawback. It shows a wrong sense of safety where there is none.

It would make sense for currentTarget tho, which is why #299 exists.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Declined The issue was declined as something which matches the TypeScript vision labels Jun 13, 2022
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Declined" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants