Is the dts lib for touchevents supported by TypeScript version 1.7.6?

Recently, I came across a post discussing whether TypeScript supports TouchEvent. The article mentioned that TypeScript 1.5.3 included declarations for HTML Touch events in lib.d.ts.

But now, with TypeScript version 1.7.6, I'm encountering an error in Visual Studio 2015 stating: Property 'changedTouches' does not exist on type 'Event.'

I tried downloading the latest jquery.TypeScript.DefinitelyTyped version="2.8.8" using NuGet but it didn't seem to work. Does anyone have any suggestions?

Update: After checking the jquery d.ts file both from NuGet and GitHub, I found them to be the same.

Code Snippet:

$('#previewEvent').on('mousedown touchstart', function(e) {
 var original = e.originalEvent;
 if (original.changedTouches && CheckMobile.isTouch_p) {
     const touchobj = original.changedTouches[0];
     swipedir = 'none';
     distX = 0;
     distY = 0;
     startX = touchobj.pageX;
     startY = touchobj.pageY;
    
     startTime = new Date().getTime();
     return !0;
} else if (!CheckMobile.isTouch_p) {
     return !0;
 }
 return !0;
});

Update: I initially misunderstood what lib.d.ts referred to, thinking it was related to jQuery when it is actually one of the libraries installed with TypeScript.

Even after updating the library, I still face the same error. Although the lib.d.ts file seems to include touch event definitions, there seems to be compatibility issues between jQuery BaseJQueryEventObject interface and TypeScript TouchEvent interface.

Any thoughts on how to fix this issue correctly? It seems like resolving the interfaces might require a third d.ts file.

Thank you...

Answer №1

When you are working in your handler function, the variable e is defined as type JQueryEventObject.

The property originalEvent originates from BaseJQueryEventObject and is of type Event. This Event eventually leads back to lib.d.ts. Everything seems clear so far.

Since the Event interface serves as a base for various derived event types (such as GamepadEvent, MutationEvent, etc.), it only includes fields that are common across all these event types. This is why accessing changedTouches is not possible, because according to TypeScript, it may not necessarily be a TouchEvent.

To address this issue, you can leverage a feature introduced in TypeScript 1.4 known as type guards.

In JavaScript, a common practice is to use typeof or instanceof to check the type of an expression at runtime. TypeScript now recognizes these conditions and adjusts type inference accordingly when used within an if block.

Given that you have assigned the same handler for both the mousedown and touchstart events, e.originalEvent could either be a MouseEvent or a TouchEvent.

Therefore, you need to handle these two scenarios:

$("#previewEvent").on("mousedown touchstart", function(e) {
    var original = e.originalEvent;
    if(original instanceof TouchEvent) {
        // Processing the touchstart event
    } else if(original instanceof MouseEvent) {
        // Handling the mousedown event
    }
});

A notable advantage, as mentioned in the quote, is that you will receive appropriate autocompletion support within each branch of the if statement.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to execute JavaScript function by clicking

I've tried everything, but I can't seem to change the button text when selecting an item in the "Requirements" dropdown. You can view the issue on this site. Located at the bottom of the page is the "Requirements" dropdown. Each item has an oncl ...

"Implementing JavaScript Validation for Textboxes: A Step-by-Step Guide

I need some help with restricting the input of a textbox within a gridview to only 'X' or 'O'. I am not very familiar with javascript, so any guidance on how to accomplish this would be greatly appreciated. It's worth noting that t ...

Upon submission of user-entered data, save it in an HTML table by creating an array variable for each column of the table

There is an HTML table containing multiple rows where users can input details such as Sprint, Role, Project, and Comments. The remaining fields, SID and Project Code, are fetched from the backend when the user clicks on the getDetails button. Users have th ...

Performing an AJAX call in Joomla version 3.3

In my view/tmpl/edit.php, I have the following code snippet: <script type="text/javascript"> jQuery("#button").click(function(){ jQuery.ajax({ url:'somefile.php', type:'POST', data: ...

Can I go back to the initial JQuery callback?

I need help with a function to load an external page. The Page URL is grabbed from the DIV id. $(".contentArea").load($('.contentArea').attr('id'), function(){ After the page loaded, I encountered a datatable and added it to the callb ...

When pressing the next or previous button on the slider, an error message pops up saying "$curr[action] is not a

I found this interesting Js fiddle that I am currently following: http://jsfiddle.net/ryt3nu1v/10/ This is my current result: https://i.sstatic.net/VygSy.png My project involves creating a slider to display different ages from an array, such as 15, 25, ...

Leveraging the power of jQuery's ajax function within Asp.Net

I am relatively new to ASP and I am having trouble figuring out the correct URL to use for an ajax call in my application. When I enter http://localhost:someport/ in the browser, it is displayed. However, when I try adding specific extensions like ["index" ...

Do TypeScript project references provide value when noEmit is used?

To enhance the speed of my editor interaction and reduce the time taken by tsc to run on my TypeScript code, I am considering implementing project references. Many teams have reported substantial performance improvements after incorporating project referen ...

Updated to TSC version 2.0.0- VS Code syntax checker unable to resolve node modules

The Issue at Hand https://i.sstatic.net/MH9Xp.png Encountering difficulties in displaying import information for NPM modules in VS Code. Errors like [TS] Cannot find module 'lodash'/'react'/'etc' occur. No problems occur wh ...

Can you explain how TypeScript module resolution handles global values?

After installing @types/jest, I noticed that jest's corresponding typescript definitions were detected automatically. However, when I started implementing integration tests with cypress (which uses mocha), I encountered a problem where mocha type defi ...

How can I pass the dynamically generated ID from PHP to AJAX/jQuery using an anchor tag?

I'm seeking help with jQuery and Ajax as I am new to it. My issue is that I have multiple 'edit' buttons in a table, one for each row's data. When I click on an edit button to modify the data, they all open at once instead of just the s ...

Having trouble locating the custom interface name in a TypeScript custom npm package?

I have an Angular application in TypeScript that is built with webpack. I also have an npm package (hosted in Sinopia, not on npm) structured as follows: my-custom-npm-package - index.ts - studentStorage.ts - student.d.ts student.d.ts interface IStudent ...

What is the best way to choose multiple elements within an Angular application?

<div class="modalmenu" > <div class="modal_itm" (mouseenter)="sepin(sepinout)" (mouseleave)="sepout(sepinout)"><a href="#in">HOME</a></div> <div class="sep" # ...

I am experiencing an issue with my jQuery loop code not functioning properly when using the .each method within the loop

I am struggling with the following code. <input type="text" name="1" class = "inp<?=$p?>"> <input type="text" name="2" class = "inp<?=$p?>"> <input type="text" name="3" class = "inp<?=$p?>"> <input type="text" na ...

Having trouble binding all elements with the JQuery .on() method in a Phonegap app running on Android 4.0?

I have encountered an issue with my code. It seems to be working well on iOS devices and newer versions of Android, as all image elements are clickable. However, on Android 4.0, only the elements that are visible on the screen when the page is loaded are c ...

Activate Jquery when the screen width is below 500 pixels

Seeking help with a code snippet that displays content upon clicking. I want this functionality to only be available when the screen or device width is 500px maximum. Can anyone guide me on how to achieve this? $(function () { $('.row1').hid ...

Having trouble with Vue i18n and TypeScript: "The '$t' property is not recognized on the 'VueConstructor' type." Any suggestions on how to resolve this issue?

Within my project, some common functions are stored in separate .ts files. Is there a way to incorporate i18n in these cases? // for i18n import Vue from 'vue' declare module 'vue/types/vue' { interface VueConstructor { $t: an ...

Sorting time using jQuery in 'hh min' format

I am facing an issue with sorting a table row that contains time in the 'hh min' format. I am attempting to convert it into minutes using the following jquery formula: RowtoSort.find('td:not(:first)').sort(function(a, b) { ...

Using Javascript and JQuery to create an alert that pops up upon loading the page is not effective

I am having trouble making an alert show up when my website loads. The Javascript code is functional when directly included in the HTML, but once I move it to a separate file named script.js and link it, nothing happens. Can someone please help me identify ...

Struggling to retrieve the ID from the API within the Angular and .NET Core component

Currently, I am working on a test project to enhance my knowledge of Angular. However, I have encountered an issue where the student's id fetched from the service is null. To handle the data, I have implemented a StudentController. Below is a snippet ...