Creating type definitions in Typescript for extensions in jQuery libraries

I have developed some custom jQuery extensions in JavaScript. They are accessed as follows:

var myNumber = $.myStaticFunction(myString);    
var myObject = $('selector').myElementFunction(myString);

In order to use these functions in the TypeScript section, I can modify the index.d.ts file of jQuery:

interface JQuery<TElement extends Node = HTMLElement> extends Iterable<TElement> {

  ...
  myElementFunction: (sr:string) => {foo:number, bar:string};
  ...

interface JQueryStatic<TElement extends Node = HTMLElement> {        
  ...
  myStaticFunction: (sr:string) => number;
  ...

I realize that directly modifying the index.d.ts may not be the best approach. Perhaps I should create a separate myextension.d.ts file instead. How should I write the definition file for jQuery extensions?

Answer №1

No need to modify the existing definition files as Typescript supports interface merging. Simply create your own myextension.d.ts file and include it using /// reference.

// myextension.d.ts
interface JQuery<TElement extends Node = HTMLElement> extends Iterable<TElement> {
    myElementFunction: (sr: string) => { foo: number, bar: string };
}

interface JQueryStatic<TElement extends Node = HTMLElement> {
    myStaticFunction: (sr: string) => number;
}


//usage.ts
/// <reference path="myextension.d.ts" />
var myNumber = $.myStaticFunction("myString");    
var myObject = $('selector').myElementFunction("myString");

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

Generate more drop-down menus populated with the same options as the initial drop-down menu

How can I dynamically add select boxes with unique IDs based on the value selected in another select box? If option 3 is chosen, 3 new select boxes should be created. If option 2 is chosen, 2 new select boxes should be created. ... I hope this makes sense ...

What is the proper way to utilize variables in package.json with npm version 7.x.x?

I am looking to utilize npm scripts to access keys found in the directories section. "directories": { "client": "client", "server": "server" }, "scripts": { "test:client&qu ...

Can you tell me what jQuery append sends back?

This is both a genuine question and a bit of a rant. Let's say I have the following html code: <ol id="t"></ol> and this jQuery code: var x = $('#t').append('test'); var y = x.append('<ol>hello</ol>& ...

Is there a way to turn off the highlights feature on MapHilight?

I am currently facing a challenge that has me stumped, and I am hoping you can provide some guidance. I'm working on a page located at: Here's the issue: I am focusing solely on the states of Washington and Idaho at the moment, and I want users ...

Spinning Divs with JQuery

I have successfully rotated a div using the following code snippet: function AnimateRotate(d){ var elem = $("#arrow"); $({deg: 0}).animate({deg: d}, { duration: 200, step: function(now){ elem.css({ tran ...

Creating Typescript libraries with bidirectional peer dependencies: A complete guide

One of my libraries is responsible for handling requests, while the other takes care of logging. Both libraries need configuration input from the client, and they are always used together. The request library makes calls to the logging library in various ...

What is your approach to converting this jQuery code to ES6 syntax?

The concept involves gathering all the links and corresponding IDs, and hiding inactive content. When a link is clicked, the associated content should be displayed. The Structure <div class="navbar"> <nav> <ul class="navTabs"> ...

Issue: The module '@nx/nx-linux-x64-gnu' is not found and cannot be located

I'm encountering issues when trying to run the build of my Angular project with NX in GitHub Actions CI. The process fails and displays errors like: npm ERR! code 1 npm ERR! path /runner/_work/myapp/node_modules/nx npm ERR! command failed npm ERR! c ...

After the click event, the variable in the Angular .ts file does not get refreshed

Great! I have a service in my .ts component that loops through an array of court names. Every time I click on a next or back arrow event, a counter is incremented starting at 0, where index 0 corresponds to field 1 and so on. The issue I'm facing is ...

php issue with parsing JSON data

After using php to create an array and then json encoding it, I encountered a strange issue. The encoded array appeared to be formed correctly in my jQuery code, but when attempting to use the data, nothing happened. My expectation was for the textbox to d ...

I am interested in verifying the presence of a user

Below is the form for checking user existence using AJAX: <!DOCTYPE html> <html> <head><title>Register new user!</title> <script src="jquery-1.7.1.min.js"></script> </head> <body> ...

What is the best way to apply a conditional class to multiple elements with jQuery?

Hey there, I'm new here so please forgive any mistakes I might make or if I break any etiquette rules! I'm trying to create some cool buttons for radio inputs on a form using field yields. The idea is to have them start off in grayscale and then ...

The TypeScript compiler is attempting to locate relative path modules in an incorrect directory

Utilizing npm link to reference a typescript library I'm working on in my testing project. As a result, the structure of my node_modules directory appears like this: node_modules/ | myLib/ | | dist/ | | | subModule/ | | | | index ...

Initiate an AJAX call using a GET request

Learning AJAX has been a bit challenging for me from the start. I went ahead and set up a database with a table called users, consisting of fields like id, name, email. Afterward, I created a php file named db.php to establish a connection with the dat ...

Stop the instantiation of type alias

Is there a way to restrict the creation of an instance of a type alias, such as ValidatedEmail? type ValidatedEmail = { address: string; validatedOn: Date } Let's say we have functions validateEmail and sendEmail. const validateEmail = (email): Valid ...

The jQuery toggle menu seems to be malfunctioning

I am experiencing an issue with a menu on my website. The menu can be viewed here: http://jsfiddle.net/paTNz/2/ The problem occurs when I try to click directly under the word "English" - the menu suddenly closes. This is confusing to me because the event ...

Best practices for implementing APIs

As someone who is relatively new to working with API in web development, I am currently building a web application using Python/Django that relies on ajax jsonp requests to the giantbomb API. Through this process, I have realized the importance of improvin ...

When trying to load an ASPX file with jQuery UI Tabs, pressing the ASP button results in an error message saying "page not found."

I am a newcomer to the world of jQuery and AJAX, facing an issue with implementing jQuery UI tabs. The problem arises when I load separate .aspx pages into each tab and try to carry out regular processing tasks without reloading the entire page. Currently, ...

Manipulating viewport settings to simulate smaller screens on a desktop device

I am working with a parent container that contains Bootstrap div.row elements holding div.col-... child elements. I am using jQuery to adjust the width and height of the container dynamically to replicate mobile device sizes for users to preview different ...

Is your CSS failing to link or display properly?

I'm encountering a frustrating issue with my code that I suspect has a simple fix, but I can't seem to figure it out. It seems like my CSS isn't linking to my HTML file properly. I've searched through various resources for a solution, b ...