Home >Web Front-end >JS Tutorial >property &#target&# does not exist on type &#HTMLInputElement&#

property &#target&# does not exist on type &#HTMLInputElement&#

DDD
DDDOriginal
2024-09-29 12:18:02390browse

property

The error "Property 'target' does not exist on type 'HTMLInputElement'" happens because TypeScript is trying to help you write safer code, and it expects you to be specific about the types of things you're working with.

Here's what it means in simple terms:

Imagine you have a toy, and you're trying to push the button, but you're pressing the wrong part of the toy. The toy says, "Hey! There's no button here!" Similarly, TypeScript is telling you, "You're trying to find the 'target' (button or object) in the wrong place!"

In web programming, when you use an event (like clicking or typing), the target is the element (like an input box) that triggered the event. If you get this error, it means you're not telling TypeScript that you're working with an event that involves the input box properly.

To fix it, you need to be clear that you're handling the event correctly, so TypeScript knows where to look for the 'target' (input box or whatever triggered the event).


To fix the error "Property 'target' does not exist on type 'HTMLInputElement'", you need to ensure that TypeScript knows that the event you're working with is of the correct type, and that event.target refers to an HTMLInputElement.

Here’s a code example that shows how to handle this properly:

Incorrect Code:

const handleChange = (event: React.FormEvent<HTMLInputElement>) => {
  console.log(event.target.value); // Error: Property 'target' does not exist on type 'HTMLInputElement'.
};

In this incorrect code, TypeScript doesn't know that event.target is an HTMLInputElement because the event is of type FormEvent, which has a more general target.

Correct Code (Solution):
To fix this, we should use event.currentTarget or explicitly cast event.target to HTMLInputElement.

Solution 1: Use event.currentTarget
currentTarget always refers to the element that the event is attached to, which in this case is the input element.

const handleChange = (event: React.FormEvent<HTMLInputElement>) => {
  const inputValue = event.currentTarget.value;
  console.log(inputValue);
};

Solution 2: Cast event.target to HTMLInputElement
Another option is to explicitly tell TypeScript that event.target is an HTMLInputElement.

const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
  const inputValue = (event.target as HTMLInputElement).value;
  console.log(inputValue);
};

Full Example:

import React, { useState } from 'react';

const InputComponent: React.FC = () => {
  const [inputValue, setInputValue] = useState('');

  const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
    setInputValue(event.currentTarget.value); // Safe, no error
  };

  return (
    <input 
      type="text"
      value={inputValue}
      onChange={handleChange}
    />
  );
};

export default InputComponent;

Explanation:
event.currentTarget ensures that you are referencing the element the event handler is attached to (the input in this case).
React.ChangeEvent is a more specific event type used for form inputs, which provides a more accurate target. This tells TypeScript that target will be an HTMLInputElement, and then .value will be available without errors.

The above is the detailed content of property &#target&# does not exist on type &#HTMLInputElement&#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn