【问题标题】:Typescript and jQuery type. The right way to import jquery type?打字稿和 jQuery 类型。导入jquery类型的正确方法?
【发布时间】:2016-02-09 04:22:45
【问题描述】:

是否可以包含 jquery.d.ts 并使用 jQueryStatic 类型?我需要类似的东西

...
protected el : jQueryStatic;
...

无论如何我都无法从https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/jquery导入jQueryStatic接口

非常感谢。

更新

/// <reference path="../../../../defs/jquery.d.ts" />
 error TS2304: Cannot find name 'jQuery'.

/// <reference path="../../../../defs/jquery.d.ts" />
import {jQuery} from 'jquery';
error TS2305: Module '"jquery"' has no exported member 'jQuery'.

更新

工作解决方案是添加类型 JQuery 而不是 jQuery。

/// <reference path="../../../defs/jquery.d.ts" />
...
protected $el: JQuery;

【问题讨论】:

    标签: javascript typescript


    【解决方案1】:

    根据您的代码:

    el : jQueryStatic
    

    由于您使用了el,我认为您的意思是element,因此正确的界面实际上是JQuery

    el : JQuery
    

    但是你问的问题

    您可以使用typeof 将变量转换为类型。更多:https://basarat.gitbooks.io/typescript/content/docs/project/declarationspaces.html

    更新

    JQuery(不是这种情况)是全局声明的。以下工作正常:

    import * as $ from 'jquery';
    var el:JQuery;
    

    【讨论】:

      猜你喜欢
      • 2019-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-07
      • 2020-08-02
      • 2019-04-11
      • 2017-09-22
      相关资源
      最近更新 更多