Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为jquery扩展编写类型脚本类型定义_Jquery_Typescript_Typescript Typings - Fatal编程技术网

为jquery扩展编写类型脚本类型定义

为jquery扩展编写类型脚本类型定义,jquery,typescript,typescript-typings,Jquery,Typescript,Typescript Typings,我有一些用javascript编写的jquery扩展。它们的访问方式如下: var myNumber = $.myStaticFunction(myString); var myObject = $('selector').myElementFunction(myString); 要在typescript部分使用此函数,我可以更改jQuery的index.d.ts: interface JQuery<TElement extends Node = HTMLElement>

我有一些用javascript编写的
jquery
扩展。它们的访问方式如下:

var myNumber = $.myStaticFunction(myString);    
var myObject = $('selector').myElementFunction(myString);
要在typescript部分使用此函数,我可以更改jQuery的index.d.ts:

interface JQuery<TElement extends Node = HTMLElement> extends Iterable<TElement> {

  ...
  myElementFunction: (sr:string) => {foo:number, bar:string};
  ...

interface JQueryStatic<TElement extends Node = HTMLElement> {        
  ...
  myStaticFunction: (sr:string) => number;
  ...
接口JQuery扩展了Iterable{
...
myElementFunction:(sr:string)=>{foo:number,bar:string};
...
接口JQueryStatic{
...
myStaticFunction:(sr:string)=>number;
...

因此,我知道这是直接更改index.d.ts最丑陋的方法!我想我必须编写一个单独的myextension.d.ts。如何为jquery扩展名编写定义文件?

您不需要更改现有的定义文件。Typescript支持。您只需定义
myextension.d.ts
并使用
/
r即可参考

// myextension.d.ts
interface JQuery<TElement extends Node = HTMLElement> extends Iterable<TElement> {
    myElementFunction: (sr: string) => { foo: number, bar: string };
}

interface JQueryStatic<TElement extends Node = HTMLElement> {
    myStaticFunction: (sr: string) => number;
}


//usage.ts
/// <reference path="myextension.d.ts" />
var myNumber = $.myStaticFunction("myString");    
var myObject = $('selector').myElementFunction("myString");
//myextension.d.ts
接口JQuery扩展了Iterable{
myElementFunction:(sr:string)=>{foo:number,bar:string};
}
接口JQueryStatic{
myStaticFunction:(sr:string)=>number;
}
//用法
/// 
var myNumber=$.myStaticFunction(“myString”);
var myObject=$('selector').myElementFunction(“myString”);