[英]Typescript definition for existing jQuery plugin with nested function
I am converting a large JS file that has a lot of jQuery plugins to use TypeScript so I need to also create the necessary TS definition files. 我正在转换一个包含许多jQuery插件的大型JS文件以使用TypeScript,因此我还需要创建必要的TS定义文件。 I have run into a plugin that I can't figure out the definition file for.
我遇到了一个插件,无法确定其定义文件。
Example of the plugin: 插件示例:
(function ($) {
$.fn.foo = function (options) {
var settings = $.extend({
one: false,
two: true
}, options);
//do something
};
$.fn.foo.bar = function (input) {
return input;
};
})(jQuery);
Example of the usage: 用法示例:
$('#list').foo({
one: true});
var updatedHtml = $('#label').foo.bar("hello world");
TypeScript defintion (so far): TypeScript定义(到目前为止):
interface JQuery {
foo(options: any) : JQuery;
}
Based on what I have so far, the first use case is covered, but not the second. 根据我到目前为止的内容,第一个用例已涵盖,但第二个未涉及。 How do I write the definition file so that it covers both use cases?
如何编写定义文件,使其涵盖两个用例?
You can use an anonymous interface (or whatever it is officially called) to define a function and its static fields together: 您可以使用匿名接口(或任何正式调用的接口)一起定义一个函数及其静态字段:
interface JQuery {
foo: {
(options: any): JQuery;
bar: <T>(input: T) => T
}
}
See also: Implementing TypeScript interface with bare function signature plus other fields 另请参阅: 使用裸函数签名和其他字段来实现TypeScript接口
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.