• TypeScript常见类型错误 | 类型EventTarget上不存在属性 - ts中的dom类型


    TypeScript常见类型错误 | 类型EventTarget上不存在属性

    报错描述

    在TypeScript中,使用事件回调函数的参数event.target.xxx时,发生报错:类型'EventTarget上不存在属性'className'

    //ts并不清楚鼠标点击的位置`event.target`是什么类型
    const name = (event.target as HTMLElement).className
    
    • 1
    • 2

    event对象的类型为EventEvent接口表示一个DOM事件,包含了事件的基本信息,如事件类型、目标元素等。可以使用该接口来定义事件的类型。

    解决办法

    1.显示指定为dom类型

    显示的指定event.target的类型

    //方法1
    const name = (event.target as HTMLElement).className
    //方法2
    const name = (<HTMLElement>event.target).className
    
    • 1
    • 2
    • 3
    • 4

    2.更改event的类型

    type HTMLElementEvent<T extends HTMLElement> = Event & {
      target: T
    }
    
    function handleClick(event: HTMLElementEvent<HTMLElement>) {
      const name = event.target.className
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    类型解析

    • Element 是所有Document对象元素的基类,只包含一些基本属性和方法。
    • HTMLElement 是 HTML 文档中某个元素的具体类型,该类型包含了所有 HTML 元素的共有属性和方法。

    DOM类型的声明

    更具体的dom元素类型基本都继承HTMLElement,并对其进行了扩充。如果报错到xxx不存在xx类型,可以将其进行更具体化的约束。

    interface HTMLElementTagNameMap {
        "a": HTMLAnchorElement;
        "abbr": HTMLElement;
        "address": HTMLElement;
        "applet": HTMLAppletElement;
        "area": HTMLAreaElement;
        "article": HTMLElement;
        "aside": HTMLElement;
        "audio": HTMLAudioElement;
        "b": HTMLElement;
        "base": HTMLBaseElement;
        "basefont": HTMLBaseFontElement;
        "bdi": HTMLElement;
        "bdo": HTMLElement;
        "blockquote": HTMLQuoteElement;
        "body": HTMLBodyElement;
        "br": HTMLBRElement;
        "button": HTMLButtonElement;
        "canvas": HTMLCanvasElement;
        "caption": HTMLTableCaptionElement;
        "cite": HTMLElement;
        "code": HTMLElement;
        "col": HTMLTableColElement;
        "colgroup": HTMLTableColElement;
        "data": HTMLDataElement;
        "datalist": HTMLDataListElement;
        "dd": HTMLElement;
        "del": HTMLModElement;
        "details": HTMLDetailsElement;
        "dfn": HTMLElement;
        "dialog": HTMLDialogElement;
        "dir": HTMLDirectoryElement;
        "div": HTMLDivElement;
        "dl": HTMLDListElement;
        "dt": HTMLElement;
        "em": HTMLElement;
        "embed": HTMLEmbedElement;
        "fieldset": HTMLFieldSetElement;
        "figcaption": HTMLElement;
        "figure": HTMLElement;
        "font": HTMLFontElement;
        "footer": HTMLElement;
        "form": HTMLFormElement;
        "frame": HTMLFrameElement;
        "frameset": HTMLFrameSetElement;
        "h1": HTMLHeadingElement;
        "h2": HTMLHeadingElement;
        "h3": HTMLHeadingElement;
        "h4": HTMLHeadingElement;
        "h5": HTMLHeadingElement;
        "h6": HTMLHeadingElement;
        "head": HTMLHeadElement;
        "header": HTMLElement;
        "hgroup": HTMLElement;
        "hr": HTMLHRElement;
        "html": HTMLHtmlElement;
        "i": HTMLElement;
        "iframe": HTMLIFrameElement;
        "img": HTMLImageElement;
        "input": HTMLInputElement;
        "ins": HTMLModElement;
        "kbd": HTMLElement;
        "label": HTMLLabelElement;
        "legend": HTMLLegendElement;
        "li": HTMLLIElement;
        "link": HTMLLinkElement;
        "main": HTMLElement;
        "map": HTMLMapElement;
        "mark": HTMLElement;
        "marquee": HTMLMarqueeElement;
        "menu": HTMLMenuElement;
        "meta": HTMLMetaElement;
        "meter": HTMLMeterElement;
        "nav": HTMLElement;
        "noscript": HTMLElement;
        "object": HTMLObjectElement;
        "ol": HTMLOListElement;
        "optgroup": HTMLOptGroupElement;
        "option": HTMLOptionElement;
        "output": HTMLOutputElement;
        "p": HTMLParagraphElement;
        "param": HTMLParamElement;
        "picture": HTMLPictureElement;
        "pre": HTMLPreElement;
        "progress": HTMLProgressElement;
        "q": HTMLQuoteElement;
        "rp": HTMLElement;
        "rt": HTMLElement;
        "ruby": HTMLElement;
        "s": HTMLElement;
        "samp": HTMLElement;
        "script": HTMLScriptElement;
        "section": HTMLElement;
        "select": HTMLSelectElement;
        "slot": HTMLSlotElement;
        "small": HTMLElement;
        "source": HTMLSourceElement;
        "span": HTMLSpanElement;
        "strong": HTMLElement;
        "style": HTMLStyleElement;
        "sub": HTMLElement;
        "summary": HTMLElement;
        "sup": HTMLElement;
        "table": HTMLTableElement;
        "tbody": HTMLTableSectionElement;
        "td": HTMLTableDataCellElement;
        "template": HTMLTemplateElement;
        "textarea": HTMLTextAreaElement;
        "tfoot": HTMLTableSectionElement;
        "th": HTMLTableHeaderCellElement;
        "thead": HTMLTableSectionElement;
        "time": HTMLTimeElement;
        "title": HTMLTitleElement;
        "tr": HTMLTableRowElement;
        "track": HTMLTrackElement;
        "u": HTMLElement;
        "ul": HTMLUListElement;
        "var": HTMLElement;
        "video": HTMLVideoElement;
        "wbr": HTMLElement;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121

    常见事件类型的声明

        interface GlobalEventHandlersEventMap {
            "abort": UIEvent;
            "animationcancel": AnimationEvent;
            "animationend": AnimationEvent;
            "animationiteration": AnimationEvent;
            "animationstart": AnimationEvent;
            "auxclick": MouseEvent;
            "beforeinput": InputEvent;
            "blur": FocusEvent;
            "cancel": Event;
            "canplay": Event;
            "canplaythrough": Event;
            "change": Event;
            "click": MouseEvent;
            "close": Event;
            "compositionend": CompositionEvent;
            "compositionstart": CompositionEvent;
            "compositionupdate": CompositionEvent;
            "contextmenu": MouseEvent;
            "copy": ClipboardEvent;
            "cuechange": Event;
            "cut": ClipboardEvent;
            "dblclick": MouseEvent;
            "drag": DragEvent;
            "dragend": DragEvent;
            "dragenter": DragEvent;
            "dragleave": DragEvent;
            "dragover": DragEvent;
            "dragstart": DragEvent;
            "drop": DragEvent;
            "durationchange": Event;
            "emptied": Event;
            "ended": Event;
            "error": ErrorEvent;
            "focus": FocusEvent;
            "focusin": FocusEvent;
            "focusout": FocusEvent;
            "formdata": FormDataEvent;
            "gotpointercapture": PointerEvent;
            "input": Event;
            "invalid": Event;
            "keydown": KeyboardEvent;
            "keypress": KeyboardEvent;
            "keyup": KeyboardEvent;
            "load": Event;
            "loadeddata": Event;
            "loadedmetadata": Event;
            "loadstart": Event;
            "lostpointercapture": PointerEvent;
            "mousedown": MouseEvent;
            "mouseenter": MouseEvent;
            "mouseleave": MouseEvent;
            "mousemove": MouseEvent;
            "mouseout": MouseEvent;
            "mouseover": MouseEvent;
            "mouseup": MouseEvent;
            "paste": ClipboardEvent;
            "pause": Event;
            "play": Event;
            "playing": Event;
            "pointercancel": PointerEvent;
            "pointerdown": PointerEvent;
            "pointerenter": PointerEvent;
            "pointerleave": PointerEvent;
            "pointermove": PointerEvent;
            "pointerout": PointerEvent;
            "pointerover": PointerEvent;
            "pointerup": PointerEvent;
            "progress": ProgressEvent;
            "ratechange": Event;
            "reset": Event;
            "resize": UIEvent;
            "scroll": Event;
            "scrollend": Event;
            "securitypolicyviolation": SecurityPolicyViolationEvent;
            "seeked": Event;
            "seeking": Event;
            "select": Event;
            "selectionchange": Event;
            "selectstart": Event;
            "slotchange": Event;
            "stalled": Event;
            "submit": SubmitEvent;
            "suspend": Event;
            "timeupdate": Event;
            "toggle": Event;
            "touchcancel": TouchEvent;
            "touchend": TouchEvent;
            "touchmove": TouchEvent;
            "touchstart": TouchEvent;
            "transitioncancel": TransitionEvent;
            "transitionend": TransitionEvent;
            "transitionrun": TransitionEvent;
            "transitionstart": TransitionEvent;
            "volumechange": Event;
            "waiting": Event;
            "webkitanimationend": Event;
            "webkitanimationiteration": Event;
            "webkitanimationstart": Event;
            "webkittransitionend": Event;
            "wheel": WheelEvent;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
  • 相关阅读:
    信息检索(49):Learning Passage Impacts for Inverted Indexes
    Python网络爬虫详细解读
    征集 |《新程序员》专访Python之父,你最想问什么?
    MySQL(5)
    【信息融合】基于matlab BP神经网络和DS证据理论不确定性信息融合问题【含Matlab源码 2204期】
    微信小程序录音和头像上传
    【MySQL】根据MVCC和Read View分析事务的四种隔离级别在读写场景分别是如何体现其隔离性的
    2023上海应用技术大学计算机考研信息汇总
    中国的LPR改革及其意义
    pandas(四十三)Pandas实现复杂Excel的转置合并
  • 原文地址:https://blog.csdn.net/qq_41370833/article/details/133385483