代码之家  ›  专栏  ›  技术社区  ›  Everton Lenger

带有jQuery参数的PhpStorm警告

  •  1
  • Everton Lenger  · 技术社区  · 7 年前

    我使用PhpStorm,当我用JSDoc将参数定义为jQuery类型时,它会给我警告。

    我曾尝试将jQuery配置为库,但它似乎不起作用。

    这是我的代码:

    /**
     * @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");
    }
    

    编辑:

    有些方法出现在intellisense中(我不知道为什么),但不幸的是 addClass 不是他们中的一个。

    enter image description here

    2 回复  |  直到 7 年前
        1
  •  3
  •   lena    7 年前

    尝试添加JQuery类型(通过 设置|语言(&A);框架| JavaScript |库 , TypeScript社区存根 或通过运行 npm i @types/jquery 在project dir中)-这应该可以解决问题:

    enter image description here

        2
  •  1
  •   Mat Lipe    6 年前

    jQuery的Typescript使用大写的J so JQuery 而不是 jQuery .

    这样更改jsdoc可以解决此问题。

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

    否则小写 jQuery 可以通过在项目中的某个位置创建一个非常简单的typescript文件来启用。

    例如,您的文件可以命名为 jquery.d.ts 并具有以下内容。

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