Specifying onClick event type with Typescript and React.Konva

后端 未结 5 722
借酒劲吻你
借酒劲吻你 2021-02-04 23:02

I\'m trying to get rid of my tslint error Type declaration of \'any\' loses type-safety. but I\'m struggling to figure out what the correct type would be for the Ev

5条回答
  •  天涯浪人
    2021-02-04 23:48

    As posted in my update above, a potential solution would be to use Declaration Merging as suggested by @Tyler-sebastion. I was able to define two additional interfaces and add the index property on the EventTarget in this way.

    interface KonvaTextEventTarget extends EventTarget {
      index: number
    }
    
    interface KonvaMouseEvent extends React.MouseEvent {
      target: KonvaTextEventTarget
    }
    

    I then can declare the event as KonvaMouseEvent in my onclick MouseEventHandler function.

    onClick={(event: KonvaMouseEvent) => {
              makeMove(ownMark, event.target.index)
    }}
    

    I'm still not 100% if this is the best approach as it feels a bit Kludgy and overly verbose just to get past the tslint error.

提交回复
热议问题