radial9174
radial9174

Reputation: 199

Property 'target' does not exist on type 'HTMLInputElement'. TypeScript React. TS2339

The handleChange() function is called whenever the input element is changed, but can't seem to access event.target.value.

Error Message

Property 'target' does not exist on type 'HTMLInputElement'.  TS2339

handleChange()

handleChange = (event:HTMLInputElement) => {
    console.log(event);
    const { name, value } = event.target;
    this.setState({[name]: value});
};

Input Element

<input name='email' type='email' value={this.state.email} onChange={this.handleChange} required />

Upvotes: 1

Views: 2382

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074585

You have the wrong type on event. It's not an HTMLInputElement, it's an event. For change it's React.ChangeEvent<T> where T is the type of element you're using it on (HTMLInputElement in your case), so:

handleChange = (event: React.ChangeEvent<HTMLInputElement>) {
    // ...

Upvotes: 4

Related Questions