ryokan ryokan - 1 month ago 7x
TypeScript Question

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

What is the proper format?

I'm new to Typescript.


You can't really define an external function with a lambda expression - a lambda is an implementation not an interface. 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. To extend from here, you must extend the JQueryStatic interface (as defined by DefininitelyTyped), adding the extended operations.

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.