【问题标题】:Typescript definition file - HelloWorld打字稿定义文件 - HelloWorld
【发布时间】:2017-02-20 10:18:34
【问题描述】:

Newbie Question

我有一个使用 jQuery 扩展的 TypeScript 文件:

function Something() {
     $.stuff.removeAll(); 
}

我正在尝试为stuff 编写定义文件,但被卡住了。我试过这个:

declare namespace JQueryStuff {
  interface Stuff{
    removeAll(): void;
  }
}

interface JQueryStatic {
    stuff(): JQueryStuff.Stuff;
}

Visual Studio 中的 TypeScript 智能使用 "Stuff" 但不是 removeAll()

【问题讨论】:

    标签: jquery typescript typescript-typings


    【解决方案1】:

    我认为无需过多挖掘即可实现的方法是将 stuff 对象与 JQueryStatic 合并接口声明进行别名。

    interface JQueryStatic {
        stuff: JQueryStuff.Stuff;
    }
    

    如果这包括你的情况,请告诉我。

    【讨论】:

      【解决方案2】:

      方法如下 - 完整答案

      interface Stuff{
      removeAll(): void;
      add(options: {
          title: string;
          text: string;
          class_name: string;
          sticky: boolean;
         });
      }
      
      
      interface JQueryStatic {
      
         stuff: Stuff;
      
      }
      

      【讨论】:

        猜你喜欢
        • 2018-03-04
        • 2017-04-26
        • 2016-07-29
        • 1970-01-01
        • 1970-01-01
        • 2018-02-04
        • 1970-01-01
        • 1970-01-01
        • 2017-01-22
        相关资源
        最近更新 更多