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. 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? Commented Dec 19, 2017 at 11:09
  • Take a look: imgur.com/a/d1Cxo 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

Sign up to request clarification or add additional context in comments.

1 Comment

Wow, it works, but I had to put an uppercase J in JQuery. Is it the way to go?
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 {}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.