PhpStorm警告使用jQuery参数

问题描述 投票:0回答:2

我用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");
}

编辑:

有些方法会出现在智能感知(我不知道为什么),但不幸的是addClass是不是其中之一。

enter image description here

jquery phpstorm webstorm jsdoc
2个回答
3
投票

尝试添加JQuery的分型(通过设置|语言和框架| JavaScript的|库,打字稿社区存根或通过运行项目目录npm i @types/jquery) - 这应该解决的问题:

enter image description here


0
投票

原来,打字稿的jQuery使用大写Ĵ所以JQuery代替jQuery

更改jsdoc像这样解决这个问题。

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

否则,小写jQuery可以通过在项目中的某处创建一个非常简单的打字稿文件中启用。

例如您的文件可能被命名为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 {}
© www.soinside.com 2019 - 2024. All rights reserved.