-
-
Notifications
You must be signed in to change notification settings - Fork 157
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
Issue with DragEventHandler<keyof HTMLElementTagNameMap> Type #407
Comments
I don't think this fixes the typing situation you describe (at least not when I just did a quick check locally). Want to share a TypeScript REPL or something? |
Thank you for your response. 😀 I am sharing the CodeSandbox link for part of the code used in the project. In the However, when I wrote the code this way, I encountered the following error in the Type '(e: React.DragEvent<HTMLDivElement>) => void' is not assignable to type 'DragEventHandler<keyof HTMLElementTagNameMap>'.
Types of parameters 'e' and 'event' are incompatible.
Type 'DragEvent<keyof HTMLElementTagNameMap>' is not assignable to type 'DragEvent<HTMLDivElement>'.
Type 'string' is not assignable to type 'HTMLDivElement'.
Type 'string' is not assignable to type 'HTMLDivElement'.typescript(2322)
index.d.ts(1559, 9): The expected type comes from property 'onDragLeave' which is declared here on type 'IntrinsicAttributes & Omit<HTMLAttributes<keyof HTMLElementTagNameMap>, "id" | "onResize"> & { className?: string | undefined; ... 11 more ...; tagName?: keyof HTMLElementTagNameMap | undefined; } & { ...; } & RefAttributes<...>' |
Since the const handleDragLeave: DragEventHandler<keyof HTMLElementTagNameMap> = (
e: DragEvent<keyof HTMLElementTagNameMap>,
): void => {
if (e.currentTarget.contains(e.relatedTarget)) return;
setIsDragActive(false);
}; In my opinion, for greater flexibility in types, it would be better to define the React.DragEventHandler<keyof HTMLElementTagNameMap | HTMLElementTagNameMap> This will prevent type conflicts and ensure compatibility when handling drag events.🙏 |
I think you maybe meant... React.DragEventHandler<keyof HTMLElementTagNameMap | HTMLElement > But assuming I'm understanding you correctly... import { createElement, HTMLAttributes } from "react";
type Props = HTMLAttributes<keyof HTMLElementTagNameMap | HTMLElement>;
function Component(props: Props) {
return createElement("div");
}
const component = createElement(Component, {
onDragLeave: event => {
// @ts-expect-error This still errors
event.currentTarget.contains()
}
});
This works (at least for your "contains" example) but would otherwise be a breaking change for this library, I think: type Props = HTMLAttributes<HTMLElement> So can you share some code that shows how this would work? |
It definitely works well when done like this: type Props = HTMLAttributes<HTMLElement>; In the previous code, the type of
Thank you for your response. 😀 |
related to (#407) Instead of fixing the `tagName` type to `keyof HTMLElementTagNameMap`, it has been made flexible by using a generic type. This allows TypeScript to better infer `event` object types like handleDrop when a specific tagName is provided in the component. ```jsx <Panel onDrop={(e: DragEvent<HTMLDivElement>) => { if (e.currentTarget.contains(e.relatedTarget)) return; }} className={styles.PanelRow} defaultSize={20} minSize={10} tagName="div" > <div className={styles.Centered}>left</div> </Panel> ```
Fixed via #414; fix has been released in [email protected] ❤️ → ☕ givebrian.coffee |
Hello. 😀
When using the
Panel
component with event handlers such asonDragLeave
,onDragEnter
,onDrop
, andonDragOver
typed asDragEventHandler<keyof HTMLElementTagNameMap>
, a type error occurs when trying to use thecontains
method, which is a method ofHTMLElement
.Here's an example where the issue occurs:
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 bothkeyof HTMLElementTagNameMap
andHTMLElement types
?The text was updated successfully, but these errors were encountered: