我用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使用大写Ĵ所以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 {}