1

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.

1

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Can this be an anonymous function of the interface? IE: interface JQueryTotalStorage extends JQuery { ($, undefined): void; totalStorage: (key, value, options) => any; setItem: (key, value) => any; getItem: (key) => any; getAll: () => any; } ($, undefined) => { bla bla
You can declare a function with no name in an interface, which makes instances of the interface act like a functional delegate. Looking at the actual library, I don't see what you would map to an anonymous delegate - they're all identified by names.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.