diff --git a/types/jquery/index.d.ts b/types/jquery/index.d.ts index 60583af1bc..8f5ceed0ec 100644 --- a/types/jquery/index.d.ts +++ b/types/jquery/index.d.ts @@ -2411,7 +2411,7 @@ interface JQueryStatic { * @since 1.4.3 */ cssNumber: JQuery.PlainObject; - readonly fn: JQuery; + readonly fn: JQuery; fx: { /** * The rate (in milliseconds) at which animations fire. @@ -2436,7 +2436,7 @@ interface JQueryStatic { * @see {@link https://api.jquery.com/jQuery.ready/} * @since 1.8 */ - ready: JQuery.Thenable; + ready: JQuery.Thenable>; /** * A collection of properties that represent the presence of different browser features or bugs. * Intended for jQuery's internal use; specific properties may be removed when they are no longer @@ -2489,7 +2489,9 @@ interface JQueryStatic { * @since 1.0 * @since 1.4 */ - (selector_object_callback?: JQuery.Selector | JQuery.TypeOrArray | JQuery.PlainObject | JQuery | (($: JQueryStatic) => void)): JQuery; + (selector_object_callback?: JQuery.Selector | JQuery.htmlString | JQuery.TypeOrArray | JQuery | + JQuery.PlainObject | + ((this: Document, $: JQueryStatic) => void)): JQuery; /** * A multi-purpose callbacks list object that provides a powerful way to manage callback lists. * @@ -2629,7 +2631,7 @@ interface JQueryStatic { * @see {@link https://api.jquery.com/jQuery.each/} * @since 1.0 */ - each(array: ArrayLike, callback: (indexInArray: number, value: T) => false | any): ArrayLike; + each(array: ArrayLike, callback: (this: T, indexInArray: number, value: T) => false | any): ArrayLike; /** * A generic iterator function, which can be used to seamlessly iterate over both objects and arrays. * Arrays and array-like objects with a length property (such as a function's arguments object) are @@ -2640,7 +2642,7 @@ interface JQueryStatic { * @see {@link https://api.jquery.com/jQuery.each/} * @since 1.0 */ - each(obj: T, callback: (propertyName: K, valueOfProperty: T[K]) => false | any): T; + each(obj: T, callback: (this: T[K], propertyName: K, valueOfProperty: T[K]) => false | any): T; /** * Takes a string and throws an exception containing it. * @@ -2648,7 +2650,7 @@ interface JQueryStatic { * @see {@link https://api.jquery.com/jQuery.error/} * @since 1.4.1 */ - error(message: string): never; + error(message: string): any; /** * Escapes any character that has a special meaning in a CSS selector. * @@ -2719,7 +2721,7 @@ interface JQueryStatic { * @see {@link https://api.jquery.com/jQuery.extend/} * @since 1.1.4 */ - extend(deep: true, target: T, ...objects: U[]): T & U; + extend(deep: true, target: any, object1: any, ...objects: any[]): any; /** * Merge the contents of two or more objects together into the first object. * @@ -2782,7 +2784,7 @@ interface JQueryStatic { * @see {@link https://api.jquery.com/jQuery.extend/} * @since 1.0 */ - extend(target: T, ...objects: U[]): T & U; + extend(target: any, object1: any, ...objects: any[]): any; /** * Load data from the server using a HTTP GET request. * @@ -2835,7 +2837,7 @@ interface JQueryStatic { * @since 1.12 * @since 2.2 */ - get(url_settings?: string | JQuery.AjaxSettings): JQuery.jqXHR; + get(url_settings?: string | JQuery.UrlAjaxSettings): JQuery.jqXHR; /** * Load JSON-encoded data from the server using a GET HTTP request. * @@ -2868,7 +2870,7 @@ interface JQueryStatic { * @since 1.0 */ getScript(url: string, - success?: JQuery.jqXHR.DoneCallback): JQuery.jqXHR; + success?: JQuery.jqXHR.DoneCallback): JQuery.jqXHR; /** * Execute some JavaScript code globally. * @@ -2915,7 +2917,7 @@ interface JQueryStatic { * @see {@link https://api.jquery.com/jQuery.htmlPrefilter/} * @since 1.12/2.2 */ - htmlPrefilter(html: string): string; + htmlPrefilter(html: JQuery.htmlString): JQuery.htmlString; /** * Search for a specified value within an array and return its index (or -1 if not found). * @@ -3029,7 +3031,7 @@ interface JQueryStatic { * @see {@link https://api.jquery.com/jQuery.noConflict/} * @since 1.0 */ - noConflict(removeAll?: boolean): JQueryStatic; + noConflict(removeAll?: boolean): this; /** * An empty function. * @@ -3145,7 +3147,7 @@ interface JQueryStatic { * @since 1.12 * @since 2.2 */ - post(url_settings?: string | JQuery.AjaxSettings): JQuery.jqXHR; + post(url_settings?: string | JQuery.UrlAjaxSettings): JQuery.jqXHR; /** * Takes a function and returns a new one that will always have a particular context. * @@ -3178,16 +3180,7 @@ interface JQueryStatic { * @see {@link https://api.jquery.com/jQuery.queue/} * @since 1.3 */ - queue(element: Element, queueName: string, newQueue: JQuery.TypeOrArray>): JQuery; - /** - * Show the queue of functions to be executed on the matched element. - * - * @param element A DOM element to inspect for an attached queue. - * @param queueName A string containing the name of the queue. Defaults to fx, the standard effects queue. - * @see {@link https://api.jquery.com/jQuery.queue/} - * @since 1.3 - */ - queue(element: Element, queueName?: string): JQuery.Queue; + queue(element: T, queueName?: string, newQueue?: JQuery.TypeOrArray>): JQuery.Queue; /** * Handles errors thrown synchronously in functions wrapped in jQuery(). * @@ -3204,7 +3197,7 @@ interface JQueryStatic { * @see {@link https://api.jquery.com/jQuery.removeData/} * @since 1.2.3 */ - removeData(element: Element, name?: string): JQuery; + removeData(element: Element, name?: string): void; /** * Creates an object containing a set of properties ready to be used in the definition of custom animations. * @@ -3215,38 +3208,28 @@ interface JQueryStatic { * @since 1.1 */ speed(duration: JQuery.Duration, easing: string, complete: (this: TElement) => void): JQuery.EffectsOptions; - /** - * Creates an object containing a set of properties ready to be used in the definition of custom animations. - * - * @param easing A string indicating which easing function to use for the transition. - * @param complete A function to call once the animation is complete, called once per matched element. - * @see {@link https://api.jquery.com/jQuery.speed/} - * @since 1.1 - */ - speed(easing: string, complete: (this: TElement) => void): JQuery.EffectsOptions; /** * Creates an object containing a set of properties ready to be used in the definition of custom animations. * * @param duration A string or number determining how long the animation will run. - * @param easing_complete_settings A string indicating which easing function to use for the transition. - * A function to call once the animation is complete, called once per matched element. + * @param easing_complete A string indicating which easing function to use for the transition. + * A function to call once the animation is complete, called once per matched element. * @see {@link https://api.jquery.com/jQuery.speed/} * @since 1.0 * @since 1.1 */ speed(duration: JQuery.Duration, - easing_complete_settings: string | ((this: TElement) => void) | JQuery.SpeedSettings): JQuery.EffectsOptions; + easing_complete: string | ((this: TElement) => void)): JQuery.EffectsOptions; /** * Creates an object containing a set of properties ready to be used in the definition of custom animations. * - * @param duration_easing_complete_settings A string or number determining how long the animation will run. - * A string indicating which easing function to use for the transition. - * A function to call once the animation is complete, called once per matched element. + * @param duration_complete_settings A string or number determining how long the animation will run. + * A function to call once the animation is complete, called once per matched element. * @see {@link https://api.jquery.com/jQuery.speed/} * @since 1.0 * @since 1.1 */ - speed(duration_easing_complete_settings?: JQuery.Duration | string | ((this: TElement) => void) | JQuery.SpeedSettings): JQuery.EffectsOptions; + speed(duration_complete_settings?: JQuery.Duration | ((this: TElement) => void) | JQuery.SpeedSettings): JQuery.EffectsOptions; /** * Remove the whitespace from the beginning and end of a string. * @@ -3325,10 +3308,24 @@ declare namespace JQuery { // region Ajax + interface AjaxSettings extends AjaxSettingsBase { + /** + * A string containing the URL to which the request is sent. + */ + url?: string; + } + + interface UrlAjaxSettings extends AjaxSettingsBase { + /** + * A string containing the URL to which the request is sent. + */ + url: string; + } + /** * @see {@link http://api.jquery.com/jquery.ajax/#jQuery-ajax-settings} */ - interface AjaxSettings { + interface AjaxSettingsBase { /** * A set of key/value pairs that map a given dataType to its MIME type, which gets sent in the Accept * request header. This header tells the server what kind of response it will accept in return. @@ -3350,7 +3347,7 @@ declare namespace JQuery { * function will cancel the request. As of jQuery 1.5, the beforeSend option will be called regardless * of the type of request. */ - beforeSend?(this: TContext, jqXHR: jqXHR, settings: AjaxSettings): false | void; + beforeSend?(this: TContext, jqXHR: jqXHR, settings: AjaxSettingsBase): false | void; /** * If set to false, it will force requested pages not to be cached by the browser. Note: Setting cache * to false will only work correctly with HEAD and GET requests. It works by appending "_={timestamp}" @@ -3559,10 +3556,6 @@ declare namespace JQuery { * An alias for method. You should use type if you're using versions of jQuery prior to 1.9.0. */ type?: string; - /** - * A string containing the URL to which the request is sent. - */ - url?: string; /** * A username to be used with XMLHttpRequest in response to an HTTP access authentication request. */ diff --git a/types/jquery/jquery-tests.ts b/types/jquery/jquery-tests.ts index 2536144845..eef1b42fa0 100644 --- a/types/jquery/jquery-tests.ts +++ b/types/jquery/jquery-tests.ts @@ -3475,18 +3475,65 @@ function JQueryStatic() { const jq: JQueryStatic = $; } - function constructor() { - function selector_object_callback() { - const jq = $ as JQueryStatic; - // $ExpectType JQuery - jq('div'); - } + function call_signature() { + // #ExpectType JQuery + $('

', new Document()); + + // #ExpectType JQuery + $('

', { + class: 'my-div', + on: { + touchstart() { + // Do something + } + } + }); + + // #ExpectType JQuery + $('span', new HTMLElement()); + + // #ExpectType JQuery + $('span', new Document()); + + // #ExpectType JQuery + $('span', $('p')); + + // #ExpectType JQuery + $('span'); + + // #ExpectType JQuery + $('

'); + + // #ExpectType JQuery + $(new HTMLElement()); + + // #ExpectType JQuery + $([new HTMLElement()]); + + // #ExpectType JQuery + $({ foo: 'bar', hello: 'world' }); + + // #ExpectType JQuery + $($('p')); + + // #ExpectType JQuery + $(function($) { + // #ExpectType Document + this; + // #ExpectType JQueryStatic + $; + }); + + // #ExpectType JQuery + $(); } function Callbacks() { - const cb = $.Callbacks(); + // #ExpectType Callbacks + $.Callbacks('once'); - cb.add(console.log); + // #ExpectType Callbacks + $.Callbacks(); } function Event() { @@ -3496,26 +3543,468 @@ function JQueryStatic() { } } + function ajax() { + // $ExpectType jqXHR + $.ajax('url', { + cache: true + }); + } + + function ajaxPrefilter() { + // $ExpectType void + $.ajaxPrefilter('dataTypes', (options, originalOptions, jqXHR) => { + // $ExpectType AjaxSettings + options; + // $ExpectType AjaxSettings + originalOptions; + // $ExpectType jqXHR + jqXHR; + + return 'filtered'; + }); + + // $ExpectType void + $.ajaxPrefilter('dataTypes', (options, originalOptions, jqXHR) => { + // $ExpectType AjaxSettings + options; + // $ExpectType AjaxSettings + originalOptions; + // $ExpectType jqXHR + jqXHR; + }); + + // $ExpectType void + $.ajaxPrefilter((options, originalOptions, jqXHR) => { + // $ExpectType AjaxSettings + options; + // $ExpectType AjaxSettings + originalOptions; + // $ExpectType jqXHR + jqXHR; + + return 'filtered'; + }); + + // $ExpectType void + $.ajaxPrefilter((options, originalOptions, jqXHR) => { + // $ExpectType AjaxSettings + options; + // $ExpectType AjaxSettings + originalOptions; + // $ExpectType jqXHR + jqXHR; + }); + } + + function ajaxSetup() { + // $ExpectType AjaxSettings + $.ajaxSetup({ + cache: true + }); + } + + function ajaxTransport() { + // $ExpectType void + $.ajaxTransport('dataTypes', (options, originalOptions, jqXHR) => { + // $ExpectType AjaxSettings + options; + // $ExpectType AjaxSettings + originalOptions; + // $ExpectType jqXHR + jqXHR; + + return { + send(headers, completeCallback) { + // $ExpectType PlainObject + headers; + // $ExpectType SuccessCallback + completeCallback; + }, + abort() { } + }; + }); + + // $ExpectType void + $.ajaxTransport('dataTypes', (options, originalOptions, jqXHR) => { + // $ExpectType AjaxSettings + options; + // $ExpectType AjaxSettings + originalOptions; + // $ExpectType jqXHR + jqXHR; + }); + } + + function contains() { + // $ExpectType boolean + $.contains(new HTMLElement(), new HTMLElement()); + } + + function css() { + // $ExpectType any + $.css(new HTMLElement(), {}); + } + + function data() { + // $ExpectType any + $.data(new HTMLElement(), 'myKey', undefined); + + // $ExpectType "myValue" + $.data(new HTMLElement(), 'myKey', 'myValue'); + + // $ExpectType any + $.data(new HTMLElement(), 'myKey'); + + // $ExpectType any + $.data(new HTMLElement()); + } + + function dequeue() { + // $ExpectType void + $.dequeue(new HTMLElement(), 'myQueue'); + + // $ExpectType void + $.dequeue(new HTMLElement()); + } + function each() { - function arrayLike() { - $.each({ length: 3 }, (index, val) => { - index === 3; - }); - } + // $ExpectType ArrayLike + $.each(['myVal1', 'myVal2'], function(index, val) { + // $ExpectType string + this; + // $ExpectType number + index; + // $ExpectType string + val; + + return false; + }); + + // $ExpectType ArrayLike + $.each(['myVal1', 'myVal2'], function(index, val) { + // $ExpectType string + this; + // $ExpectType number + index; + // $ExpectType string + val; + + return 10; + }); + + // $ExpectType ArrayLike + $.each(['myVal1', 'myVal2'], function(index, val) { + // $ExpectType string + this; + // $ExpectType number + index; + // $ExpectType string + val; + }); + + // $ExpectType { myVal1: boolean; myVal2: () => 10; myVal3: string; } + $.each({ + myVal1: false, + myVal2: () => { + return 10; + }, + myVal3: 'myVal3' + }, function(propertyName, valueOfProperty) { + // $ExpectType string | boolean | (() => 10) + this; + // $ExpectType "myVal1" | "myVal2" | "myVal3" + propertyName; + // $ExpectType string | boolean | (() => 10) + valueOfProperty; + + return false; + }); + + // $ExpectType { myVal1: boolean; myVal2: () => 10; myVal3: string; } + $.each({ + myVal1: false, + myVal2: () => { + return 10; + }, + myVal3: 'myVal3' + }, function(propertyName, valueOfProperty) { + // $ExpectType string | boolean | (() => 10) + this; + // $ExpectType "myVal1" | "myVal2" | "myVal3" + propertyName; + // $ExpectType string | boolean | (() => 10) + valueOfProperty; + + return 10; + }); + + // $ExpectType { myVal1: boolean; myVal2: () => 10; myVal3: string; } + $.each({ + myVal1: false, + myVal2: () => { + return 10; + }, + myVal3: 'myVal3' + }, function(propertyName, valueOfProperty) { + // $ExpectType string | boolean | (() => 10) + this; + // $ExpectType "myVal1" | "myVal2" | "myVal3" + propertyName; + // $ExpectType string | boolean | (() => 10) + valueOfProperty; + }); + } + + function error() { + jQuery.error = console.error; + } + + function escapeSelector() { + // $ExpectType string + $.escapeSelector('span'); + } + + function extend() { + const t = { name: 'myObj' }; + const u = new EventTarget(); + const v = new Node(); + const w = new Comment(); + const x = new Text(); + const y = new Element(); + const z = new HTMLElement(); + const a = new SVGElement(); + + // $ExpectType { name: string; } & EventTarget & Node & Comment & Text & Element & HTMLElement + $.extend(true, t, u, v, w, x, y, z); + + // $ExpectType { name: string; } & EventTarget & Node & Comment & Text & Element + $.extend(true, t, u, v, w, x, y); + + // $ExpectType { name: string; } & EventTarget & Node & Comment & Text + $.extend(true, t, u, v, w, x); + + // $ExpectType { name: string; } & EventTarget & Node & Comment + $.extend(true, t, u, v, w); + + // $ExpectType { name: string; } & EventTarget & Node + $.extend(true, t, u, v); + + // $ExpectType { name: string; } & EventTarget + $.extend(true, t, u); + + // $ExpectType any + $.extend(true, t, u, v, w, x, y, z, a); + + // $ExpectType { name: string; } & EventTarget & Node & Comment & Text & Element & HTMLElement + $.extend(t, u, v, w, x, y, z); + + // $ExpectType { name: string; } & EventTarget & Node & Comment & Text & Element + $.extend(t, u, v, w, x, y); + + // $ExpectType { name: string; } & EventTarget & Node & Comment & Text + $.extend(t, u, v, w, x); + + // $ExpectType { name: string; } & EventTarget & Node & Comment + $.extend(t, u, v, w); + + // $ExpectType { name: string; } & EventTarget & Node + $.extend(t, u, v); + + // $ExpectType { name: string; } & EventTarget + $.extend(t, u); + + // $ExpectType any + $.extend(t, u, v, w, x, y, z, a); + } + + function get() { + // $ExpectType jqXHR + $.get('url', { myData: 'myData' }, (data, textStatus, jqXHR) => { + // $ExpectType any + data; + // $ExpectType SuccessTextStatus + textStatus; + // $ExpectType jqXHR + jqXHR; + }, 'script'); + + // $ExpectType jqXHR + $.get('url', 'myData', (data, textStatus, jqXHR) => { + // $ExpectType any + data; + // $ExpectType SuccessTextStatus + textStatus; + // $ExpectType jqXHR + jqXHR; + }, 'script'); + + // $ExpectType jqXHR + $.get('url', { myData: 'myData' }, null, 'script'); + + // $ExpectType jqXHR + $.get('url', 'myData', null, 'script'); + + // $ExpectType jqXHR + $.get('url', (data, textStatus, jqXHR) => { + // $ExpectType any + data; + // $ExpectType SuccessTextStatus + textStatus; + // $ExpectType jqXHR + jqXHR; + }, 'script'); + + // $ExpectType jqXHR + $.get('url', null, 'script'); + + // $ExpectType jqXHR + $.get('url', (data, textStatus, jqXHR) => { + // $ExpectType any + data; + // $ExpectType SuccessTextStatus + textStatus; + // $ExpectType jqXHR + jqXHR; + }, 'script'); + + // $ExpectType jqXHR + $.get('url', { myData: 'myData' }); + + // $ExpectType jqXHR + $.get('url', 'myData'); + + // $ExpectType jqXHR + $.get('url'); + + // $ExpectType jqXHR + $.get({ url: 'url' }); + + // $ExpectType jqXHR + $.get(); + } + + function getJSON() { + // $ExpectType jqXHR + $.getJSON('url', { myVal1: 'myVal1' }, (data, textStatus, jqXHR) => { + // $ExpectType any + data; + // $ExpectType SuccessTextStatus + textStatus; + // $ExpectType jqXHR + jqXHR; + }); + + // $ExpectType jqXHR + $.getJSON('url', 'myVal1', (data, textStatus, jqXHR) => { + // $ExpectType any + data; + // $ExpectType SuccessTextStatus + textStatus; + // $ExpectType jqXHR + jqXHR; + }); + + // $ExpectType jqXHR + $.getJSON('url', (data, textStatus, jqXHR) => { + // $ExpectType any + data; + // $ExpectType SuccessTextStatus + textStatus; + // $ExpectType jqXHR + jqXHR; + }); + + // $ExpectType jqXHR + $.getJSON('url', { myVal1: 'myVal1' }); + + // $ExpectType jqXHR + $.getJSON('url', 'myVal1'); + + // $ExpectType jqXHR + $.getJSON('url'); + } + + function getScript() { + // $ExpectType jqXHR + $.getScript('url', (data, textStatus, jqXHR) => { + // $ExpectType string | undefined + data; + // $ExpectType SuccessTextStatus + textStatus; + // $ExpectType jqXHR + jqXHR; + }); + } + + function globalEval() { + // $ExpectType void + $.globalEval('throw new Error();'); + } + + function grep() { + // $ExpectType string[] + $.grep(['myVal1', 'myVal2'], (elementOfArray, indexInArray) => { + // $ExpectType string + elementOfArray; + // $ExpectType number + indexInArray; + + return true; + }, true); + + // $ExpectType string[] + $.grep(['myVal1', 'myVal2'], (elementOfArray, indexInArray) => { + // $ExpectType string + elementOfArray; + // $ExpectType number + indexInArray; + + return true; + }); + } + + function hasData() { + // $ExpectType boolean + $.hasData(new HTMLElement()); + } + + function holdReady() { + // $ExpectType void + $.holdReady(true); + } + + function htmlPrefilter() { + // $ExpectType string + $.htmlPrefilter(''); + } + + function inArray() { + // $ExpectType number + $.inArray(1, [1, 2], 1); + + // $ExpectType number + $.inArray(1, [1, 2]); } function isArray() { function type_guard(obj: object) { if ($.isArray(obj)) { - console.log(obj[0]); + // $ExpectType any[] + obj; } } } + function isEmptyObject() { + // $ExpectType boolean + $.isEmptyObject({}); + } + function isFunction() { function type_guard(obj: object) { if ($.isFunction(obj)) { - obj(); + // $ExpectType Function + obj; } } } @@ -3523,7 +4012,8 @@ function JQueryStatic() { function isNumeric() { function type_guard(obj: boolean) { if ($.isNumeric(obj)) { - obj.toFixed(); + // $ExpectType (true & number) | (false & number) + obj; } } } @@ -3531,7 +4021,8 @@ function JQueryStatic() { function isPlainObject() { function type_guard(obj: object) { if ($.isPlainObject(obj)) { - obj['key'] = true; + // $ExpectType PlainObject + obj; } } } @@ -3539,37 +4030,239 @@ function JQueryStatic() { function isWindow() { function type_guard(obj: object) { if ($.isWindow(obj)) { - obj.location.href === 'href'; + // $ExpectType Window + obj; } } } + function isXMLDoc() { + // $ExpectType boolean + $.isXMLDoc(new Node()); + } + + function makeArray() { + // $ExpectType number[] + $.makeArray([1, 2]); + } + function map() { - function object() { - const testObj = { - myProp: true, - name: 'Rogers', - }; + // $ExpectType number[] + $.map([1, 2, 3], (elementOfArray, indexInArray) => { + // $ExpectType number + elementOfArray; + // $ExpectType number + indexInArray; - const results = $.map(testObj, (propertyOfObject, key) => { - switch (key) { - case 'myProp': - return 1; - case 'name': - return false; - } - }); + return 200 + 10; + }); - for (const result of results) { - result === 1; + // $ExpectType (false | 1)[] + $.map({ + myProp: true, + name: 'Rogers', + }, (propertyOfObject, key) => { + // $ExpectType string | boolean + propertyOfObject; + // $ExpectType "myProp" | "name" + key; + + switch (key) { + case 'myProp': + return 1; + case 'name': + return false; } - } + }); + } + + function merge() { + // $ExpectType (string | number)[] + $.merge([1, 2, 3], ['myVal1', 'myVal2']); + } + + function noConflict() { + // $ExpectType JQueryStatic + $.noConflict(true); + + // $ExpectType JQueryStatic + $.noConflict(); + } + + function noop() { + // $ExpectType undefined + $.noop(); + } + + function now() { + // $ExpectType number + $.now(); + } + + function param() { + // $ExpectType string + $.param([true, 20], true); + + // $ExpectType string + $.param({ + myVal1: true, + myVal2: 20 + }, true); + + // $ExpectType string + $.param($('input'), true); + + // $ExpectType string + $.param([true, 20]); + + // $ExpectType string + $.param({ + myVal1: true, + myVal2: 20 + }); + + // $ExpectType string + $.param($('input')); + } + + function parseHTML() { + // $ExpectType Node[] + $.parseHTML('', document, true); + + // $ExpectType Node[] + $.parseHTML('', null, true); + + // $ExpectType Node[] + $.parseHTML('', undefined, true); + + // $ExpectType Node[] + $.parseHTML('', document); + + // $ExpectType Node[] + $.parseHTML('', null); + + // $ExpectType Node[] + $.parseHTML('', undefined); + + // $ExpectType Node[] + $.parseHTML('', false); + + // $ExpectType Node[] + $.parseHTML(''); + } + + function parseJSON() { + // $ExpectType any + $.parseJSON('{}'); + } + + function parseXML() { + // $ExpectType XMLDocument + $.parseXML(''); + } + + function post() { + // $ExpectType jqXHR + $.post('url', { myData: 'myData' }, (data, textStatus, jqXHR) => { + // $ExpectType any + data; + // $ExpectType SuccessTextStatus + textStatus; + // $ExpectType jqXHR + jqXHR; + }, 'script'); + + // $ExpectType jqXHR + $.post('url', 'myData', (data, textStatus, jqXHR) => { + // $ExpectType any + data; + // $ExpectType SuccessTextStatus + textStatus; + // $ExpectType jqXHR + jqXHR; + }, 'script'); + + // $ExpectType jqXHR + $.post('url', { myData: 'myData' }, null, 'script'); + + // $ExpectType jqXHR + $.post('url', 'myData', null, 'script'); + + // $ExpectType jqXHR + $.post('url', (data, textStatus, jqXHR) => { + // $ExpectType any + data; + // $ExpectType SuccessTextStatus + textStatus; + // $ExpectType jqXHR + jqXHR; + }, 'script'); + + // $ExpectType jqXHR + $.post('url', null, 'script'); + + // $ExpectType jqXHR + $.post('url', (data, textStatus, jqXHR) => { + // $ExpectType any + data; + // $ExpectType SuccessTextStatus + textStatus; + // $ExpectType jqXHR + jqXHR; + }, 'script'); + + // $ExpectType jqXHR + $.post('url', { myData: 'myData' }); + + // $ExpectType jqXHR + $.post('url', 'myData'); + + // $ExpectType jqXHR + $.post('url'); + + // $ExpectType jqXHR + $.post({ url: 'url' }); + + // $ExpectType jqXHR + $.post(); + } + + function proxy() { + // $ExpectType Function + $.proxy($.noop, {}, 1, 2); + + // $ExpectType Function + $.proxy($.noop, {}); + + // $ExpectType Function + $.proxy({ myFunc: $.noop }, 'myFunc', 1, 2); + + // $ExpectType Function + $.proxy({ myFunc: $.noop }, 'myFunc'); } function queue() { - const el = new HTMLElement(); - const queue = jQuery.queue(el); - queue[0] === 'inprogress'; + // $ExpectType Queue + $.queue(new HTMLElement(), 'myQueue', function(next) { + // $ExpectType HTMLElement + this; + // $ExpectType () => void + next; + }); + + // $ExpectType Queue + $.queue(new HTMLElement(), 'myQueue', [function(next) { + // $ExpectType HTMLElement + this; + // $ExpectType () => void + next; + }]); + + // $ExpectType Queue + $.queue(new HTMLElement(), 'myQueue'); + + // $ExpectType Queue + $.queue(new HTMLElement()); } function readyException() { @@ -3577,6 +4270,94 @@ function JQueryStatic() { console.error(error); }; } + + function removeData() { + // $ExpectType void + $.removeData(new HTMLElement(), 'test1'); + + // $ExpectType void + $.removeData(new HTMLElement()); + } + + function speed() { + // $ExpectType EffectsOptions + $.speed(5000, 'linear', function() { + // $ExpectType HTMLElement + this; + }); + + // $ExpectType EffectsOptions + $.speed(5000, 'linear'); + + // $ExpectType EffectsOptions + $.speed(5000, function() { + // $ExpectType HTMLElement + this; + }); + + // $ExpectType EffectsOptions + $.speed(5000); + + // $ExpectType EffectsOptions + $.speed(function() { + // $ExpectType HTMLElement + this; + }); + + // $ExpectType EffectsOptions + $.speed({ + duration: 5000, + easing: 'linear', + complete() { + // $ExpectType HTMLElement + this; + } + }); + + // $ExpectType EffectsOptions + $.speed(); + } + + function trim() { + // $ExpectType string + $.trim('myStr'); + } + + function type() { + // $ExpectType "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function" | "error" | "array" | "date" | "null" | "regexp" + $.type({}); + } + + function unique() { + // $ExpectType HTMLElement[] + $.unique([new HTMLElement()]); + } + + function uniqueSort() { + // $ExpectType HTMLElement[] + $.uniqueSort([new HTMLElement()]); + } + + function when() { + const t = $.ajax() as JQuery.jqXHR; + const u = $.ajax() as JQuery.jqXHR; + const v = $.ajax() as JQuery.jqXHR; + + // $ExpectType Promise<[string | number | boolean, string, jqXHR], any, any> + $.when(t, u, v); + + // $ExpectType Promise<[string | number, string, jqXHR], any, any> + $.when(t, u); + + // $ExpectType Promise, any, any> + $.when(t); + + // $ExpectType Promise + $.when($.Deferred()); + + // $ExpectType Promise + $.when(); + } } function EffectsOptions() { diff --git a/types/jquery/test/example-tests.ts b/types/jquery/test/example-tests.ts index bbb5d15d6c..f0e2e197f6 100644 --- a/types/jquery/test/example-tests.ts +++ b/types/jquery/test/example-tests.ts @@ -2434,7 +2434,7 @@ function examples() { } function jQuery_error_0() { - jQuery.error = console.error as any; + jQuery.error = console.error; } function jQuery_escape_selector_0() {