Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a generic (void) JQueryDeferred object using TypeScript and jquery.d.ts

I am using the jquery.d.ts that comes from the DefinitelyTyped repo (using tsd). It seems to be the "official" jquery typing as the license header says it is created by Microsoft.

Now, I occasionally need to create a deferred object that does not need to pass any arguments to it's .resolve() or .reject() methods and I can't figure out how do this with the static typing.

I am currently using a workaround at this point: I am creating the deferred via JQueryDeferred<boolean> dfd = $.Deferred() and resolve it via dfd.resolve(false) although I wouldn't really need to pass a boolean value. From the jquery.d.ts file I can only find a JQueryDeferred<T> declartion, but not a generic one (sth. like JQueryDeferred<void> but this is not valid TypeScript syntax).

How is it done "the proper way" (TM)?

like image 567
Dynalon Avatar asked Mar 20 '23 12:03

Dynalon


1 Answers

You have to include the generic <void> in the call to jQuery.Deferred<void>() as well.

The following code should compile without errors:

var dfd: JQueryDeferred<void> = jQuery.Deferred<void>();
dfd.resolve();

I don't know why JQueryDeferred<boolean> dfd = $.Deferred() doesn't cause an error as well though.

By the way: Even if you stay with your workaround, you don't have to pass a boolean to dfd.resolve() since resolve's parameter value is optional (as indicated by the ?).

like image 88
Dennis Korbar Avatar answered Mar 22 '23 17:03

Dennis Korbar