Does TypeScript support TouchEvent?

后端 未结 3 1004
别跟我提以往
别跟我提以往 2021-01-08 00:31

UPDATE

TypeScript 1.5.3 added declarations for HTML Touch events to lib.d.ts

I can see that it supports UIEven

相关标签:
3条回答
  • 2021-01-08 01:09

    thank you for the advice. I went to the Touch Events Specification W3C Working Draft 05 May 2011 and created a set of ambient declarations from that. There is a more recent candidate recommendation, but this is what I think is actually implemented in most browsers. This seems to work well.

    PS: the declare var TouchEvent at the bottom is not part of the w3c draft. It is an adaptation of the same code for UIEvent that is part of the standard interfaces shipped with TypeScript.

    interface Touch {
        identifier:number;
        target:EventTarget;
        screenX:number;
        screenY:number;
        clientX:number;
        clientY:number;
        pageX:number;
        pageY:number;
    };
    
    interface TouchList {
        length:number;
        item (index:number):Touch;
        identifiedTouch(identifier:number):Touch;
    };
    
    interface TouchEvent extends UIEvent {
        touches:TouchList;
        targetTouches:TouchList;
        changedTouches:TouchList;
        altKey:bool;
        metaKey:bool;
        ctrlKey:bool;
        shiftKey:bool;
        initTouchEvent (type:string, canBubble:bool, cancelable:bool, view:AbstractView, detail:number, ctrlKey:bool, altKey:bool, shiftKey:bool, metaKey:bool, touches:TouchList, targetTouches:TouchList, changedTouches:TouchList);
    };
    
    declare var TouchEvent: {
        prototype: TouchEvent;
        new(): TouchEvent;
    }
    
    0 讨论(0)
  • 2021-01-08 01:20

    Here is a addendum. This code adds type event handlers to HTMLElement. You can add this after the code that defines the TouchEvent interface;

    //
    // add touch events to HTMLElement
    //
    interface HTMLElement extends Element, MSHTMLElementRangeExtensions, ElementCSSInlineStyle, MSEventAttachmentTarget, MSHTMLElementExtensions, MSNodeExtensions {
        ontouchstart: (ev: TouchEvent) => any;
        ontouchmove: (ev: TouchEvent) => any;
        ontouchend: (ev: TouchEvent) => any;
        ontouchcancel: (ev: TouchEvent) => any;
    }
    
    0 讨论(0)
  • 2021-01-08 01:27

    You have several options here:

    First option: Extend UIEvent to have the members you'd expect on a TouchEvent (and only use them in touch event listeners):

    interface UIEvent {
        targetTouches: { pageX: number; pageY: number; }[];
        // etc...
    }
    

    Second option: Define your own TouchEvent interface and add a type assertion when binding the event

    interface TouchEvent {
        (event: TouchEventArgs): void;
    }
    
    interface TouchEventArgs {
        targetTouches: { pageX: number; pageY: number; }[];
        // etc
    }
    
     theElement.addEventListener("touchstart", <UIEvent>((theTouchEvent:TouchEvent) => alert("touchy")));
    
    0 讨论(0)
提交回复
热议问题