【发布时间】:2018-06-02 05:02:50
【问题描述】:
每次在 jQuery 循环中初始化 $(this) 时,我们似乎都会在 PhpStorm 中遇到这个错误。经过数小时/数天的研究,我没有找到任何有关 PhpStorm 为何将其标记为错误的信息。
我们使用 typescript-eslint-parser 进行 linting,这不会标记任何错误。
这只是 PhpStorm 的问题,还是真正的 TypeScript 错误?
可以解决还是忽略?
这些是安装的类型:
"@types/axios": "^0.14.0",
"@types/bootstrap": "3.3.36",
"@types/node": "8.5.1",
"@types/jquery": "3.2.7",
"@types/qs": "^6.5.1",
$.fn.extend({
performanceCharts: function (): JQuery {
'use strict'
$(this).each(function (): void {
let $this: JQuery = $(this)
});
}
})
$(document).ready(function () {
$('.js-performance-charts').performanceCharts()
})
<div class="js-performance-charts"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
【问题讨论】:
-
对我来说,PhpStorm 和 tsc 编译器都会为您的代码报告 2 个错误:
missing return statement for non-void method(TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.) 和unresolved function(TS2339: Property 'performanceCharts' does not exist on type 'JQuery<HTMLElement>'.)。我看不到Initializer type JQuery<TElement> is not assignable to variable type JQuery错误(PhpStorm 2017.3.1,内置 173.3942) -
感谢您测试代码,但这只是我们插件的快速 sn-p 以显示代码应该做什么,这就是为什么我错过的事情有错误(对不起) .在我将 PhpStorm 从 2017.1 更新到 2017.3 之后(在发现您的版本号之后),我已经解决了这个问题。感谢您的帮助。
标签: jquery typescript phpstorm webstorm typescript-typings