dhysong
dhysong

Reputation: 1021

How to extend JQuery 3 selector in TypeScript

After upgrading JQuery from 2 to 3, the latest TypeScript definition no longer contains:

expr: any;

Our code base extends the JQuery selector to provide a case insensitive selector:

    $.extend($.expr[":"], {
        containsCaseInsensitive: (elem: any, i: number, match: any, array: any) =>
            (elem.textContent || elem.innerText || "").toLowerCase().indexOf((match[3] || "").toLowerCase()) >= 0
    });

With the latest TS definition, this no longer transpiles. Is there a way to provide the same functionality in JQuery 3 without manually updating the TS definition for JQuery?

Upvotes: 1

Views: 113

Answers (1)

Leonard Thieu
Leonard Thieu

Reputation: 823

The code posted now works as of @types/[email protected] (fixed by DefinitelyTyped/DefinitelyTyped#29503).


For older versions, you would've had to add the following declaration to your project.

interface JQueryStatic {
    expr: any;
}

This declaration would merge with the declaration of JQueryStatic from @types/jquery and make expr available.

Upvotes: 2

Related Questions