Skip to content

Commit

Permalink
[jquery] Accept Document, Window, and JQuery.PlainObject for th…
Browse files Browse the repository at this point in the history
  • Loading branch information
leonard-thieu committed Oct 9, 2018
1 parent 8310859 commit cf0be56
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
10 changes: 5 additions & 5 deletions types/jquery/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ $( "button" ).click( function() {
</html>
```
*/
data<T extends string | number | boolean | symbol | object | null>(element: Element, key: string, value: T): T;
data<T extends string | number | boolean | symbol | object | null>(element: Element | Document | Window | JQuery.PlainObject, key: string, value: T): T;
/**
* Returns value at named data store for the element, as set by `jQuery.data(element, name, value)`, or
* the full data store for the element.
Expand All @@ -490,7 +490,7 @@ $( "button" ).click( function() {
// `unified-signatures` is disabled so that behavior when passing `undefined` to `value` can be documented. Unifying the signatures
// results in potential confusion for users from an unexpected parameter.
// tslint:disable-next-line:unified-signatures
data(element: Element, key: string, value: undefined): any;
data(element: Element | Document | Window | JQuery.PlainObject, key: string, value: undefined): any;
/**
* Returns value at named data store for the element, as set by `jQuery.data(element, name, value)`, or
* the full data store for the element.
Expand Down Expand Up @@ -540,7 +540,7 @@ $( "span:last" ).text( jQuery.data( div, "test" ).last );
</html>
```
*/
data(element: Element, key?: string): any;
data(element: Element | Document | Window | JQuery.PlainObject, key?: string): any;
/**
* Execute the next function on the queue for the matched element.
*
Expand Down Expand Up @@ -1976,7 +1976,7 @@ $p.append( jQuery.hasData( p ) + " " ); // false
</html>
```
*/
hasData(element: Element): boolean;
hasData(element: Element | Document | Window | JQuery.PlainObject): boolean;
/**
* Holds or releases the execution of jQuery's ready event.
*
Expand Down Expand Up @@ -12944,7 +12944,7 @@ $( "span:eq(3)" ).text( "" + jQuery.data( div, "test2" ) );
</html>
```
*/
removeData(element: Element, name?: string): void;
removeData(element: Element | Document | Window | JQuery.PlainObject, name?: string): void;
/**
* Creates an object containing a set of properties ready to be used in the definition of custom animations.
*
Expand Down
22 changes: 14 additions & 8 deletions types/jquery/jquery-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,21 +284,23 @@ function JQueryStatic() {
}

function data() {
const element: Element | Document | Window | JQuery.PlainObject = {} as any;

const value: string | undefined = {} as any;
// $ExpectError
$.data(new HTMLElement(), 'myKey', value);
$.data(element, 'myKey', value);

// $ExpectType "myValue"
$.data(new HTMLElement(), 'myKey', 'myValue');
$.data(element, 'myKey', 'myValue');

// $ExpectType any
$.data(new HTMLElement(), 'myKey', undefined);
$.data(element, 'myKey', undefined);

// $ExpectType any
$.data(new HTMLElement(), 'myKey');
$.data(element, 'myKey');

// $ExpectType any
$.data(new HTMLElement());
$.data(element);
}

function dequeue() {
Expand Down Expand Up @@ -611,8 +613,10 @@ function JQueryStatic() {
}

function hasData() {
const element: Element | Document | Window | JQuery.PlainObject = {} as any;

// $ExpectType boolean
$.hasData(new HTMLElement());
$.hasData(element);
}

function holdReady() {
Expand Down Expand Up @@ -1731,11 +1735,13 @@ function JQueryStatic() {
}

function removeData() {
const element: Element | Document | Window | JQuery.PlainObject = {} as any;

// $ExpectType void
$.removeData(new HTMLElement(), 'test1');
$.removeData(element, 'test1');

// $ExpectType void
$.removeData(new HTMLElement());
$.removeData(element);
}

function speed() {
Expand Down

0 comments on commit cf0be56

Please sign in to comment.