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

Types for values on events #2491

Open
ersinakinci opened this issue Apr 10, 2024 · 3 comments
Open

Types for values on events #2491

ersinakinci opened this issue Apr 10, 2024 · 3 comments
Labels
enhancement New feature or request.

Comments

@ersinakinci
Copy link

What is the feature you are proposing?

Screenshot 2024-04-10 at 12 44 07 PM

It would be nice to have event.target.value as a type that can be accessed.

@ersinakinci ersinakinci added the enhancement New feature or request. label Apr 10, 2024
@yusukebe
Copy link
Member

Hi @ersinakinci

I recommend using a type guard to add a type to the target.

<div>
  <input
    onChange={(e) => {
      if (e.currentTarget instanceof HTMLInputElement) {
        e.currentTarget.value // ...
      }
    }}
  />
</div>

But, indeed, it will be inconvenient, and it has the proper type with React.

@usualoma What do you think about it? Do you think we should implement it?

@fzn0x
Copy link
Contributor

fzn0x commented May 6, 2024

In React we can do something like this @yusukebe :

import { ChangeEvent } from "react";

const validateField = (event: ChangeEvent<HTMLInputElement>) {
 console.log(event.target.value);
}

I think it's something good to implement because it was close to Dom.

@fzn0x
Copy link
Contributor

fzn0x commented May 6, 2024

As an alternative we can use type guard or type annotation:

import { ChangeEvent } from "react";

const validateField = (event: Event) {
 console.log((event.target as HTMLInputElement).value);
}

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

No branches or pull requests

3 participants