带有 jQuery 个参数的 PhpStorm 警告

PhpStorm warnings with jQuery parameters

我使用 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 不是其中之一。

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

原来 jQuery 的 Typescript 使用大写 J,所以 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 {}