1

I use PhpStorm and it is giving me warnings when I define a parameter as a type jQuery with JSDoc.

I've tried to configure jQuery as a library, but it didn't seem to work.

This is my code:

/**
 * @param {jQuery} $button
 */
function setActive($button)
{
    // The code is working fine, but PhpStorm is giving the warning 
    // "Unresolved function or method addClass()" here.
    $button.addClass("active");

    // If I try to do this, no warning is displayed.
    $("#test").addClass("test");
}

EDIT:

Some methods appear in the intellisense (I'm not sure why), but unfortunately addClass isn't one of them.

enter image description here

9
  • That's odd. It recognizes the parameter as jQuery for me.
    – Phiter
    Commented Dec 19, 2017 at 10:58
  • @Phiter Do you have any special configuration in your PHPStorm? It seems correct, but I just can't get rid of this warning. Commented Dec 19, 2017 at 11:07
  • Nothing special. If you use $('body'). will it autocomplete with jQuery functions?
    – Phiter
    Commented Dec 19, 2017 at 11:09
  • Take a look: imgur.com/a/d1Cxo
    – Phiter
    Commented Dec 19, 2017 at 11:11
  • Yes, it works with anything inside $(). It doesn't work only with the parameter. Commented Dec 19, 2017 at 11:14

2 Answers 2

3

Try adding JQuery typings (either via Settings | Languages & Frameworks | JavaScript | Libraries, TypeScript Community Stubs or by running npm i @types/jquery in project dir) - this should solve the issue:

enter image description here

1
  • 1
    Wow, it works, but I had to put an uppercase J in JQuery. Is it the way to go? Commented Dec 19, 2017 at 11:51
1

Turns out the Typescript for jQuery uses an Uppercase J so JQuery instead of jQuery.

Changing the jsdoc like so fixes this issue.

/**
 * @param {JQuery} $button
 */

Otherwise the lowercase jQuery may be enabled by creating a very simple typescript file somewhere in your project.

For example your file could be named jquery.d.ts and have the following content.

/**
 * Must have the @types/jquery typescript installed
 * either via PHPStorm -> Javascript -> Libraries
 * or `npm i @types/jquery`
 */
interface jQuery<TElement = HTMLElement> extends JQuery {}

Not the answer you're looking for? Browse other questions tagged or ask your own question.