react-resize-panel icon indicating copy to clipboard operation
react-resize-panel copied to clipboard

Issue with DragEventHandler<keyof HTMLElementTagNameMap> Type

Open in-ch opened this issue 1 year ago • 0 comments

Hello. 😀

When using the Panel component with event handlers such as onDragLeave, onDragEnter, onDrop, and onDragOver typed as DragEventHandler<keyof HTMLElementTagNameMap>, a type error occurs when trying to use the contains method, which is a method of HTMLElement.

Here's an example where the issue occurs:

const handleDragLeave: DragEventHandler<keyof HTMLElementTagNameMap> = (
  e: DragEvent<keyof HTMLElementTagNameMap>,
): void => {
  if (e.currentTarget.contains(e.relatedTarget)) return;
  setIsDragActive(false);
};

스크린샷 2024-09-12 12 20 00

The issue is that the keyof HTMLElementTagNameMap type represents string keys for tag names, so it cannot infer the types of methods (e.g., contains) that the event object has.

Therefore, the DragEventHandler<keyof HTMLElementTagNameMap> type is limited in using methods of HTMLElement.

How about using a union type or something similar to make it compatible with both keyof HTMLElementTagNameMap and HTMLElement types?

in-ch avatar Sep 12 '24 03:09 in-ch