0

Question: Is there a way to combine the types of different HTML DOM events in Typescript?

(I know I can avoid this error by splitting my function in two different functions but I want to avoid that)

My component looks similar to this:

const TestComponent: React.FC = () => {
const handleFileChange = (e: React.DragEvent | React.SyntheticEvent) => {
  const fileData = e.dataTransfer || e.target;
});

return (
  <div>
    <div onDrop={handleFileChange} />
    <input type="file" onChange={handleFileChange} />
  </div>
)};

The error I'm getting is on the e.dataTransfer inside the handleFileChange function:

Property 'dataTransfer' does not exist on type 'SyntheticEvent<Element, Event> | DragEvent<Element>'.
Property 'dataTransfer' does not exist on type 'SyntheticEvent<Element, Event>'.

I understand this error and why this is happening, but I can't find a workaround to do this with typescript, any docs reference I should be looking at?

2 Answers 2

1

Your types are fine, you just need to check if dataTransfer exists before using it:

const handleFileChange = (e: React.DragEvent | React.SyntheticEvent) => {
  const fileData = 'dataTransfer' in e ? e.dataTransfer : e.target;
};
Sign up to request clarification or add additional context in comments.

Comments

1

You can try something like this.

const TestComponent: React.FC = () => {

  // you can use any value you want as `type`
  const handleFileChange = (type: "input" | "drop", e: React.DragEvent | React.SyntheticEvent) => {
    if (type === "drop") {
      const fileData = (e as React.DragEvent).dataTransfer
    }

    if (type === "input") {
      const target = e.target
    }

    // if there are any other events
  }

  return (
    <div>
      <div onDrop={handleFileChange.bind(null, "drop")} />
      <input type="file" onChange={handleFileChange.bind(null, "input")} />
    </div>
  )
}

Just bind a type to the event so with each event type you can decide what you want to do.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.