简体   繁体   English

Typescript中带有参数的匿名函数

[英]Anonymous Functions with parameters in Typescript

I'm trying to create an anonymous function in typescript with parameters I'm trying the following format: 我试图在打字稿中使用参数创建匿名函数,并尝试以下格式:

(function ($, undefined) => {})(jQuery);

I'm trying to create a definition file for an existing js libary called jQuery-total-storage.js 我正在尝试为一个名为jQuery-total-storage.js的现有js库创建一个定义文件

What is the proper format? 什么是正确的格式?

I'm new to Typescript. 我是Typescript的新手。

You can't really define an external function with a lambda expression - a lambda is an implementation not an interface. 您不能真正使用lambda表达式定义外部函数-lambda是实现而不是接口。 It can't be used to only express a contract, because it also expresses how that contract is implemented. 它不能仅用于表达合同,因为它还表达了合同的实施方式。

If you need to define an external function, you need to define one as part of an interface. 如果需要定义外部功能,则需要定义一个作为接口的一部分。


Looking at the jQuery Total Storage library, it seems to extend from the jQuery singleton. 看一下jQuery Total Storage库,它似乎是从jQuery单例扩展的。 To extend from here, you must extend the JQueryStatic interface (as defined by DefininitelyTyped ), adding the extended operations. 要从此处扩展,必须扩展JQueryStatic接口(由DefininitelyTyped定义),并添加扩展的操作。

interface JQueryStatic {
    totalStorage(key: string, value: any): void;
    totalStorage(key: string): any;
}

Interfaces are just strongly-typed declarations. 接口只是强类型的声明。 In this case, it's adding type information to the external library. 在这种情况下,它将类型信息添加到外部库中。

To be clear: you can only add type information, you can't change the signatures. 要清楚: 您只能添加类型信息,不能更改签名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM