【问题标题】:Initializer type JQuery<TElement> is not assignable to variable type JQuery初始化器类型 JQuery<TElement> 不可分配给变量类型 JQuery
【发布时间】: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&lt;HTMLElement&gt;'.)。我看不到 Initializer type JQuery&lt;TElement&gt; 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


【解决方案1】:

已解决:这一定是 PhpStorm 版本的问题。从2017.1升级到2017.3后问题就消失了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-01
    • 1970-01-01
    • 2020-04-02
    • 2019-06-02
    • 1970-01-01
    • 2018-01-04
    • 2020-11-09
    • 1970-01-01
    相关资源
    最近更新 更多