Skip to content
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

Event currentTarget always typed as Node #1794

Closed
daformat opened this issue Jul 18, 2024 · 2 comments
Closed

Event currentTarget always typed as Node #1794

daformat opened this issue Jul 18, 2024 · 2 comments

Comments

@daformat
Copy link

daformat commented Jul 18, 2024

I'm using Konva with React. I'm noticing the type of KonvaEventObject is:

export interface KonvaEventObject<EventType> {
    type: string;
    target: Shape | Stage;
    evt: EventType;
    pointerId: number;
    currentTarget: Node;
    cancelBubble: boolean;
    child?: Node;
}

Hence, the currentTarget is always a Node. Wouldn't it make sense to add another generic argument so that we can specify the exact type of the currentTarget (for example a Rect or any other)?

Something like:

export interface KonvaEventObject<EventType, EventTarget = Node> {
    type: string;
    target: Shape | Stage;
    evt: EventType;
    pointerId: number;
    currentTarget: EventTarget;
    cancelBubble: boolean;
    child?: Node;
}

I was hoping that the type KonvaEventListener would provide me with a way to do that, but the current type is:

export type KonvaEventListener<This, EventType> = (this: This, ev: KonvaEventObject<EventType>) => void;

So this includes a first argument which is this and it doesn't actually enforece the type of currentTarget.
What I would need would be (with the previous modification of the KonvaEventObject type) would be:

export type KonvaEventListener<EventTarget, EventType> = (ev: KonvaEventObject<EventType, EventTarget>) => void;

The reason for that is that:

  • right now I can't specify the type of the currentTarget, and have to resort to check the type in each listener
  • when declaring an event listener function, I'd like a generic type to describe it instead of always having to write the same (event: KonvaEventObject) => void thing.

Maybe I'm missing something though, thoughts and comments welcome 🙏

@daformat
Copy link
Author

FIY the current solution I'm using is to define my own KonvaEventListener:

export type KonvaEventListener<EventTarget, EventType> = (
  event: KonvaEventObject<EventType> & {
    currentTarget: EventTarget;
  }
) => void;

@lavrton
Copy link
Member

lavrton commented Jul 18, 2024

Fixed. Will release later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants