diff --git a/types/jquery/README.md b/types/jquery/README.md index d1bbad05fc..8fab451ce0 100644 --- a/types/jquery/README.md +++ b/types/jquery/README.md @@ -9,7 +9,7 @@ When jQuery is globally available, you can use `jQuery` and `$` directly. When you want to import jQuery as a module and have a global DOM available (e.g. browser and browser-like environments): ```typescript -import * as jQuery from 'jquery'; +import jQuery = require('jquery'); ``` #### Importing (without a global DOM available) @@ -23,6 +23,21 @@ const jQuery = jQueryFactory(window, true); Note that while the factory function ignores the second parameter, it is required to get correct type declarations. +### Project structure + +- [jquery-tests.ts](jquery-tests.ts) + - Tests that exercise TypeScript-specific usage and cases not covered by other test files. +- [test/example-tests.ts](test/example-tests.ts) + - Tests generated from examples in jQuery documentation. +- [test/longdesc-tests.ts](test/longdesc-tests.ts) + - Tests generated from non-example snippets in jQuery documentation. +- [test/jquery-window-module-tests.ts](test/jquery-window-module-tests.ts)
+ [test/jquery-slim-window-module-tests.ts](test/jquery-slim-window-module-tests.ts) + - Tests importing jQuery with a DOM available +- [test/jquery-no-window-module-tests.ts](test/jquery-no-window-module-tests.ts)
+ [test/jquery-slim-no-window-module-tests.ts](test/jquery-slim-no-window-module-tests.ts) + - Tests importing jQuery without a DOM available + ### Authoring type definitions for jQuery plugins `$.fn` is represented by `JQuery`. diff --git a/types/jquery/index.d.ts b/types/jquery/index.d.ts index 2c7d5ce75a..ce32477d98 100644 --- a/types/jquery/index.d.ts +++ b/types/jquery/index.d.ts @@ -62,7 +62,7 @@ interface JQuery { * @see {@link https://api.jquery.com/add/} * @since 1.4 */ - add(selector: JQuery.Selector, context: Element): JQuery; + add(selector: JQuery.Selector, context: Element): this; /** * Create a new jQuery object with elements added to the set of matched elements. * @@ -74,7 +74,7 @@ interface JQuery { * @since 1.0 * @since 1.3.2 */ - add(selector: JQuery.Selector | JQuery.TypeOrArray | JQuery.htmlString | JQuery): JQuery; + add(selector: JQuery.Selector | JQuery.TypeOrArray | JQuery.htmlString | JQuery): this; /** * Add the previous set of elements on the stack to the current set, optionally filtered by a selector. * @@ -103,7 +103,7 @@ interface JQuery { * @see {@link https://api.jquery.com/after/} * @since 1.0 */ - after(...contents: Array | JQuery>): this; + after(...contents: Array | JQuery>): this; /** * Insert content, specified by the parameter, after each element in the set of matched elements. * @@ -115,7 +115,7 @@ interface JQuery { * @since 1.4 * @since 1.10 */ - after(fn: (this: TElement, index: number, html: string) => JQuery.htmlString | JQuery.TypeOrArray | JQuery): this; + after(fn: (this: TElement, index: number, html: string) => JQuery.htmlString | JQuery.TypeOrArray | JQuery): this; /** * Register a handler to be called when Ajax requests complete. This is an AjaxEvent. * @@ -219,7 +219,7 @@ interface JQuery { * @see {@link https://api.jquery.com/append/} * @since 1.0 */ - append(...contents: Array | JQuery>): this; + append(...contents: Array | JQuery>): this; /** * Insert content, specified by the parameter, to the end of each element in the set of matched elements. * @@ -230,7 +230,7 @@ interface JQuery { * @see {@link https://api.jquery.com/append/} * @since 1.4 */ - append(fn: (this: TElement, index: number, html: string) => JQuery.htmlString | JQuery.TypeOrArray | JQuery): this; + append(fn: (this: TElement, index: number, html: string) => JQuery.htmlString | JQuery.TypeOrArray | JQuery): this; /** * Insert every element in the set of matched elements to the end of the target. * @@ -277,7 +277,7 @@ interface JQuery { * @see {@link https://api.jquery.com/before/} * @since 1.0 */ - before(...contents: Array | JQuery>): this; + before(...contents: Array | JQuery>): this; /** * Insert content, specified by the parameter, before each element in the set of matched elements. * @@ -289,7 +289,7 @@ interface JQuery { * @since 1.4 * @since 1.10 */ - before(fn: (this: TElement, index: number, html: string) => JQuery.htmlString | JQuery.TypeOrArray | JQuery): this; + before(fn: (this: TElement, index: number, html: string) => JQuery.htmlString | JQuery.TypeOrArray | JQuery): this; // [bind() overloads] https://github.com/jquery/api.jquery.com/issues/1048 /** * Attach a handler to an event for the elements. @@ -426,14 +426,14 @@ interface JQuery { * @since 1.3 * @since 1.6 */ - closest(selector: JQuery.Selector | JQuery | Element): this; + closest(selector: JQuery.Selector | Element | JQuery): this; /** * Get the children of each element in the set of matched elements, including text and comment nodes. * * @see {@link https://api.jquery.com/contents/} * @since 1.2 */ - contents(): this; + contents(): JQuery; /** * Bind an event handler to the "contextmenu" JavaScript event, or trigger that event on an element. * @@ -620,7 +620,7 @@ interface JQuery { * @see {@link https://api.jquery.com/each/} * @since 1.0 */ - each(fn: (this: TElement, index: number, element: Element) => void | false): this; + each(fn: (this: TElement, index: number, element: TElement) => void | false): this; /** * Remove all child nodes of the set of matched elements from the DOM. * @@ -653,7 +653,7 @@ interface JQuery { * @see {@link https://api.jquery.com/jQuery.fn.extend/} * @since 1.0 */ - extend(obj: object): JQuery; + extend(obj: object): this; /** * Display the matched elements by fading them to opaque. * @@ -796,7 +796,7 @@ interface JQuery { * @since 1.0 * @since 1.6 */ - find(selector: JQuery.Selector | Element | JQuery): JQuery; + find(selector: JQuery.Selector | Element | JQuery): this; /** * Stop the currently-running animation, remove all queued animations, and complete all animations for * the matched elements. @@ -985,7 +985,7 @@ interface JQuery { * @since 1.0 * @since 1.4 */ - index(element?: Element | JQuery | JQuery.Selector): number; + index(element?: JQuery.Selector | Element | JQuery): number; /** * Set the CSS inner height of each element in the set of matched elements. * @@ -1058,7 +1058,7 @@ interface JQuery { * @since 1.0 * @since 1.6 */ - is(selector: JQuery.Selector | ((this: TElement, index: number, element: TElement) => boolean) | JQuery | Element | Element[]): boolean; + is(selector: JQuery.Selector | JQuery.TypeOrArray | JQuery | ((this: TElement, index: number, element: TElement) => boolean)): boolean; /** * Bind an event handler to the "keydown" JavaScript event, or trigger that event on an element. * @@ -1148,7 +1148,7 @@ interface JQuery { * @see {@link https://api.jquery.com/map/} * @since 1.2 */ - map(callback: (this: TElement, index: number, domElement: TElement) => any | any[] | null | undefined): JQuery; + map(callback: (this: TElement, index: number, domElement: TElement) => any | any[] | null | undefined): this; /** * Bind an event handler to the "mousedown" JavaScript event, or trigger that event on an element. * @@ -1309,7 +1309,7 @@ interface JQuery { * @since 1.0 * @since 1.4 */ - not(selector: JQuery.Selector | JQuery.TypeOrArray | ((this: TElement, index: number, element: TElement) => boolean) | JQuery): this; + not(selector: JQuery.Selector | JQuery.TypeOrArray | JQuery | ((this: TElement, index: number, element: TElement) => boolean)): this; /** * Remove an event handler. * @@ -1629,7 +1629,7 @@ interface JQuery { * @see {@link https://api.jquery.com/prepend/} * @since 1.0 */ - prepend(...contents: Array | JQuery>): this; + prepend(...contents: Array | JQuery>): this; /** * Insert content, specified by the parameter, to the beginning of each element in the set of matched elements. * @@ -1640,7 +1640,7 @@ interface JQuery { * @see {@link https://api.jquery.com/prepend/} * @since 1.4 */ - prepend(fn: (this: TElement, index: number, html: string) => JQuery.htmlString | JQuery.TypeOrArray | JQuery): this; + prepend(fn: (this: TElement, index: number, html: string) => JQuery.htmlString | JQuery.TypeOrArray | JQuery): this; /** * Insert every element in the set of matched elements to the beginning of the target. * @@ -1688,7 +1688,7 @@ interface JQuery { * @see {@link https://api.jquery.com/promise/} * @since 1.6 */ - promise(type: string, target: T): T & JQuery.Promise; + promise(type: string, target: T): T & JQuery.Promise; /** * Return a Promise object to observe when all actions of a certain type bound to the collection, * queued or not, have finished. @@ -1697,7 +1697,7 @@ interface JQuery { * @see {@link https://api.jquery.com/promise/} * @since 1.6 */ - promise(target: T): T & JQuery.Promise; + promise(target: T): T & JQuery.Promise; /** * Return a Promise object to observe when all actions of a certain type bound to the collection, * queued or not, have finished. @@ -1751,7 +1751,7 @@ interface JQuery { * @see {@link https://api.jquery.com/pushStack/} * @since 1.3 */ - pushStack(elements: ArrayLike, name: string, args: any[]): JQuery; + pushStack(elements: ArrayLike, name: string, args: any[]): this; /** * Add a collection of DOM elements onto the jQuery stack. * @@ -1759,7 +1759,7 @@ interface JQuery { * @see {@link https://api.jquery.com/pushStack/} * @since 1.0 */ - pushStack(elements: ArrayLike): JQuery; + pushStack(elements: ArrayLike): this; /** * Manipulate the queue of functions to be executed, once for each matched element. * @@ -1858,7 +1858,7 @@ interface JQuery { * @since 1.2 * @since 1.4 */ - replaceWith(newContent: JQuery.htmlString | JQuery.TypeOrArray | JQuery | ((this: TElement) => any)): JQuery; + replaceWith(newContent: JQuery.htmlString | JQuery | JQuery.TypeOrArray | ((this: TElement) => any)): this; /** * Bind an event handler to the "resize" JavaScript event, or trigger that event on an element. * @@ -2388,7 +2388,7 @@ interface JQuery { * @since 1.2 * @since 1.4 */ - wrapInner(wrappingElement: JQuery.Selector | JQuery.htmlString | Element | JQuery | ((this: TElement, index: number) => string | Element | JQuery)): this; + wrapInner(wrappingElement: JQuery.Selector | JQuery.htmlString | Element | JQuery | ((this: TElement, index: number) => string | JQuery | Element)): this; } interface JQuery extends ArrayLike, Iterable { } @@ -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. * @@ -3065,7 +3067,7 @@ interface JQueryStatic { * @see {@link https://api.jquery.com/jQuery.parseHTML/} * @since 1.8 */ - parseHTML(data: string, context: Document | null | undefined, keepScripts: boolean): Node[]; + parseHTML(data: string, context: Document | null | undefined, keepScripts: boolean): JQuery.Node[]; /** * Parses a string into an array of DOM nodes. * @@ -3075,7 +3077,7 @@ interface JQueryStatic { * @see {@link https://api.jquery.com/jQuery.parseHTML/} * @since 1.8 */ - parseHTML(data: string, context_keepScripts?: Document | null | undefined | boolean): Node[]; + parseHTML(data: string, context_keepScripts?: Document | null | undefined | boolean): JQuery.Node[]; /** * Takes a well-formed JSON string and returns the resulting JavaScript value. * @@ -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. * @@ -3298,6 +3281,7 @@ interface JQueryStatic { declare namespace JQuery { type TypeOrArray = T | T[]; + type Node = Element | Text | Comment; /** * A string is designated htmlString in jQuery documentation when it is used to represent one or more @@ -3325,262 +3309,18 @@ declare namespace JQuery { // region Ajax - /** - * @see {@link http://api.jquery.com/jquery.ajax/#jQuery-ajax-settings} - */ - interface AjaxSettings { - /** - * 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. - */ - accepts?: PlainObject; - /** - * By default, all requests are sent asynchronously (i.e. this is set to true by default). If you need - * synchronous requests, set this option to false. Cross-domain requests and dataType: "jsonp" requests - * do not support synchronous operation. Note that synchronous requests may temporarily lock the - * browser, disabling any actions while the request is active. As of jQuery 1.8, the use of async: - * false with jqXHR ($.Deferred) is deprecated; you must use the success/error/complete callback - * options instead of the corresponding methods of the jqXHR object such as jqXHR.done(). - */ - async?: boolean; - /** - * A pre-request callback function that can be used to modify the jqXHR (in jQuery 1.4.x, - * XMLHTTPRequest) object before it is sent. Use this to set custom headers, etc. The jqXHR and - * settings objects are passed as arguments. This is an Ajax Event. Returning false in the beforeSend - * 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; - /** - * 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}" - * to the GET parameters. The parameter is not needed for other types of requests, except in IE8 when a - * POST is made to a URL that has already been requested by a GET. - */ - cache?: boolean; - /** - * A function to be called when the request finishes (after success and error callbacks are executed). - * The function gets passed two arguments: The jqXHR (in jQuery 1.4.x, XMLHTTPRequest) object and a - * string categorizing the status of the request ("success", "notmodified", "nocontent", "error", - * "timeout", "abort", or "parsererror"). As of jQuery 1.5, the complete setting can accept an array of - * functions. Each function will be called in turn. This is an Ajax Event. - */ - complete?: TypeOrArray>; - /** - * An object of string/regular-expression pairs that determine how jQuery will parse the response, - * given its content type. - */ - contents?: PlainObject; - /** - * When sending data to the server, use this content type. Default is - * "application/x-www-form-urlencoded; charset=UTF-8", which is fine for most cases. If you explicitly - * pass in a content-type to $.ajax(), then it is always sent to the server (even if no data is sent). - * As of jQuery 1.6 you can pass false to tell jQuery to not set any content type header. Note: The W3C - * XMLHttpRequest specification dictates that the charset is always UTF-8; specifying another charset - * will not force the browser to change the encoding. Note: For cross-domain requests, setting the - * content type to anything other than application/x-www-form-urlencoded, multipart/form-data, or - * text/plain will trigger the browser to send a preflight OPTIONS request to the server. - */ - contentType?: string | false; - /** - * This object will be the context of all Ajax-related callbacks. By default, the context is an object - * that represents the Ajax settings used in the call ($.ajaxSettings merged with the settings passed to $.ajax). - */ - context?: TContext; - /** - * An object containing dataType-to-dataType converters. Each converter's value is a function that - * returns the transformed value of the response. - */ - converters?: PlainObject<((value: any) => any) | true>; - /** - * If you wish to force a crossDomain request (such as JSONP) on the same domain, set the value of - * crossDomain to true. This allows, for example, server-side redirection to another domain. - */ - crossDomain?: boolean; - /** - * Data to be sent to the server. It is converted to a query string, if not already a string. It's - * appended to the url for GET-requests. See processData option to prevent this automatic processing. - * Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same - * key based on the value of the traditional setting (described below). - */ - data?: PlainObject | string | any[]; - /** - * A function to be used to handle the raw response data of XMLHttpRequest. This is a pre-filtering - * function to sanitize the response. You should return the sanitized data. The function accepts two - * arguments: The raw data returned from the server and the 'dataType' parameter. - */ - dataFilter?(data: string, type: string): any; - /** - * The type of data that you're expecting back from the server. If none is specified, jQuery will try - * to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON - * will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be - * returned as a string). The available types (and the result passed as the first argument to your - * success callback) are: - * - * "xml": Returns a XML document that can be processed via jQuery. - * - * "html": Returns HTML as plain text; included script tags are evaluated when inserted in the DOM. - * - * "script": Evaluates the response as JavaScript and returns it as plain text. Disables caching by - * appending a query string parameter, _=[TIMESTAMP], to the URL unless the cache option is set to - * true. Note: This will turn POSTs into GETs for remote-domain requests. - * - * "json": Evaluates the response as JSON and returns a JavaScript object. Cross-domain "json" requests - * are converted to "jsonp" unless the request includes jsonp: false in its request options. The JSON - * data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. As of - * jQuery 1.9, an empty response is also rejected; the server should return a response of null or {} - * instead. (See json.org for more information on proper JSON formatting.) - * - * "jsonp": Loads in a JSON block using JSONP. Adds an extra "?callback=?" to the end of your URL to - * specify the callback. Disables caching by appending a query string parameter, "_=[TIMESTAMP]", to - * the URL unless the cache option is set to true. - * - * "text": A plain text string. - * - * multiple, space-separated values: As of jQuery 1.5, jQuery can convert a dataType from what it - * received in the Content-Type header to what you require. For example, if you want a text response to - * be treated as XML, use "text xml" for the dataType. You can also make a JSONP request, have it - * received as text, and interpreted by jQuery as XML: "jsonp text xml". Similarly, a shorthand string - * such as "jsonp xml" will first attempt to convert from jsonp to xml, and, failing that, convert from - * jsonp to text, and then from text to xml. - */ - dataType?: 'xml' | 'html' | 'script' | 'json' | 'jsonp' | 'text' | string; - /** - * A function to be called if the request fails. The function receives three arguments: The jqXHR (in - * jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an - * optional exception object, if one occurred. Possible values for the second argument (besides null) - * are "timeout", "error", "abort", and "parsererror". When an HTTP error occurs, errorThrown receives - * the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error." As of jQuery - * 1.5, the error setting can accept an array of functions. Each function will be called in turn. Note: - * This handler is not called for cross-domain script and cross-domain JSONP requests. This is an Ajax Event. - */ - error?: TypeOrArray>; - /** - * Whether to trigger global Ajax event handlers for this request. The default is true. Set to false to - * prevent the global handlers like ajaxStart or ajaxStop from being triggered. This can be used to - * control various Ajax Events. - */ - global?: boolean; - /** - * An object of additional header key/value pairs to send along with requests using the XMLHttpRequest - * transport. The header X-Requested-With: XMLHttpRequest is always added, but its default - * XMLHttpRequest value can be changed here. Values in the headers setting can also be overwritten from - * within the beforeSend function. - */ - headers?: PlainObject; - /** - * Allow the request to be successful only if the response has changed since the last request. This is - * done by checking the Last-Modified header. Default value is false, ignoring the header. In jQuery - * 1.4 this technique also checks the 'etag' specified by the server to catch unmodified data. - */ - ifModified?: boolean; - /** - * Allow the current environment to be recognized as "local," (e.g. the filesystem), even if jQuery - * does not recognize it as such by default. The following protocols are currently recognized as local: - * file, *-extension, and widget. If the isLocal setting needs modification, it is recommended to do so - * once in the $.ajaxSetup() method. - */ - isLocal?: boolean; - /** - * Override the callback function name in a JSONP request. This value will be used instead of - * 'callback' in the 'callback=?' part of the query string in the url. So {jsonp:'onJSONPLoad'} would - * result in 'onJSONPLoad=?' passed to the server. As of jQuery 1.5, setting the jsonp option to false - * prevents jQuery from adding the "?callback" string to the URL or attempting to use "=?" for - * transformation. In this case, you should also explicitly set the jsonpCallback setting. For example, - * { jsonp: false, jsonpCallback: "callbackName" }. If you don't trust the target of your Ajax - * requests, consider setting the jsonp property to false for security reasons. - */ - jsonp?: string | boolean; - /** - * Specify the callback function name for a JSONP request. This value will be used instead of the - * random name automatically generated by jQuery. It is preferable to let jQuery generate a unique name - * as it'll make it easier to manage the requests and provide callbacks and error handling. You may - * want to specify the callback when you want to enable better browser caching of GET requests. As of - * jQuery 1.5, you can also use a function for this setting, in which case the value of jsonpCallback - * is set to the return value of that function. - */ - jsonpCallback?: string | ((this: TContext) => string); - /** - * The HTTP method to use for the request (e.g. "POST", "GET", "PUT"). - */ - method?: string; - /** - * A mime type to override the XHR mime type. - */ - mimeType?: string; - /** - * A password to be used with XMLHttpRequest in response to an HTTP access authentication request. - */ - password?: string; - /** - * By default, data passed in to the data option as an object (technically, anything other than a - * string) will be processed and transformed into a query string, fitting to the default content-type - * "application/x-www-form-urlencoded". If you want to send a DOMDocument, or other non-processed data, - * set this option to false. - */ - processData?: boolean; - /** - * Only applies when the "script" transport is used (e.g., cross-domain requests with "jsonp" or - * "script" dataType and "GET" type). Sets the charset attribute on the script tag used in the request. - * Used when the character set on the local page is not the same as the one on the remote script. - */ - scriptCharset?: string; - /** - * An object of numeric HTTP codes and functions to be called when the response has the corresponding - * code. - * - * If the request is successful, the status code functions take the same parameters as the success - * callback; if it results in an error (including 3xx redirect), they take the same parameters as the error callback. - */ - statusCode?: PlainObject | Ajax.ErrorCallback>; - /** - * A function to be called if the request succeeds. The function gets passed three arguments: The data - * returned from the server, formatted according to the dataType parameter or the dataFilter callback - * function, if specified; a string describing the status; and the jqXHR (in jQuery 1.4.x, - * XMLHttpRequest) object. As of jQuery 1.5, the success setting can accept an array of functions. Each - * function will be called in turn. This is an Ajax Event. - */ - success?: TypeOrArray>; - /** - * Set a timeout (in milliseconds) for the request. A value of 0 means there will be no timeout. This - * will override any global timeout set with $.ajaxSetup(). The timeout period starts at the point the - * $.ajax call is made; if several other requests are in progress and the browser has no connections - * available, it is possible for a request to time out before it can be sent. In jQuery 1.4.x and - * below, the XMLHttpRequest object will be in an invalid state if the request times out; accessing any - * object members may throw an exception. In Firefox 3.0+ only, script and JSONP requests cannot be - * cancelled by a timeout; the script will run even if it arrives after the timeout period. - */ - timeout?: number; - /** - * Set this to true if you wish to use the traditional style of param serialization. - */ - traditional?: boolean; - /** - * An alias for method. You should use type if you're using versions of jQuery prior to 1.9.0. - */ - type?: string; + interface AjaxSettings extends Ajax.AjaxSettingsBase { /** * A string containing the URL to which the request is sent. */ url?: string; + } + + interface UrlAjaxSettings extends Ajax.AjaxSettingsBase { /** - * A username to be used with XMLHttpRequest in response to an HTTP access authentication request. + * A string containing the URL to which the request is sent. */ - username?: string; - /** - * Callback for creating the XMLHttpRequest object. Defaults to the ActiveXObject when available (IE), - * the XMLHttpRequest otherwise. Override to provide your own implementation for XMLHttpRequest or - * enhancements to the factory. - */ - xhr?(): XMLHttpRequest; - /** - * An object of fieldName-fieldValue pairs to set on the native XHR object. - * - * In jQuery 1.5, the withCredentials property was not propagated to the native XHR and thus CORS - * requests requiring it would ignore this flag. For this reason, we recommend using jQuery 1.5.1+ - * should you require the use of it. - */ - xhrFields?: PlainObject; + url: string; } namespace Ajax { @@ -3593,12 +3333,421 @@ declare namespace JQuery { } interface ErrorCallback { - (this: TContext, jqXHR: jqXHR, textStatus: ErrorTextStatus | null, errorThrown: string): void; + (this: TContext, jqXHR: jqXHR, textStatus: ErrorTextStatus, errorThrown: string): void; } interface CompleteCallback { (this: TContext, jqXHR: jqXHR, textStatus: TextStatus): void; } + + /** + * @see {@link http://api.jquery.com/jquery.ajax/#jQuery-ajax-settings} + */ + 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. + */ + accepts?: PlainObject; + /** + * By default, all requests are sent asynchronously (i.e. this is set to true by default). If you need + * synchronous requests, set this option to false. Cross-domain requests and dataType: "jsonp" requests + * do not support synchronous operation. Note that synchronous requests may temporarily lock the + * browser, disabling any actions while the request is active. As of jQuery 1.8, the use of async: + * false with jqXHR ($.Deferred) is deprecated; you must use the success/error/complete callback + * options instead of the corresponding methods of the jqXHR object such as jqXHR.done(). + */ + async?: boolean; + /** + * A pre-request callback function that can be used to modify the jqXHR (in jQuery 1.4.x, + * XMLHTTPRequest) object before it is sent. Use this to set custom headers, etc. The jqXHR and + * settings objects are passed as arguments. This is an Ajax Event. Returning false in the beforeSend + * 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: 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}" + * to the GET parameters. The parameter is not needed for other types of requests, except in IE8 when a + * POST is made to a URL that has already been requested by a GET. + */ + cache?: boolean; + /** + * A function to be called when the request finishes (after success and error callbacks are executed). + * The function gets passed two arguments: The jqXHR (in jQuery 1.4.x, XMLHTTPRequest) object and a + * string categorizing the status of the request ("success", "notmodified", "nocontent", "error", + * "timeout", "abort", or "parsererror"). As of jQuery 1.5, the complete setting can accept an array of + * functions. Each function will be called in turn. This is an Ajax Event. + */ + complete?: TypeOrArray>; + /** + * An object of string/regular-expression pairs that determine how jQuery will parse the response, + * given its content type. + */ + contents?: PlainObject; + /** + * When sending data to the server, use this content type. Default is + * "application/x-www-form-urlencoded; charset=UTF-8", which is fine for most cases. If you explicitly + * pass in a content-type to $.ajax(), then it is always sent to the server (even if no data is sent). + * As of jQuery 1.6 you can pass false to tell jQuery to not set any content type header. Note: The W3C + * XMLHttpRequest specification dictates that the charset is always UTF-8; specifying another charset + * will not force the browser to change the encoding. Note: For cross-domain requests, setting the + * content type to anything other than application/x-www-form-urlencoded, multipart/form-data, or + * text/plain will trigger the browser to send a preflight OPTIONS request to the server. + */ + contentType?: string | false; + /** + * This object will be the context of all Ajax-related callbacks. By default, the context is an object + * that represents the Ajax settings used in the call ($.ajaxSettings merged with the settings passed to $.ajax). + */ + context?: TContext; + /** + * An object containing dataType-to-dataType converters. Each converter's value is a function that + * returns the transformed value of the response. + */ + converters?: PlainObject<((value: any) => any) | true>; + /** + * If you wish to force a crossDomain request (such as JSONP) on the same domain, set the value of + * crossDomain to true. This allows, for example, server-side redirection to another domain. + */ + crossDomain?: boolean; + /** + * Data to be sent to the server. It is converted to a query string, if not already a string. It's + * appended to the url for GET-requests. See processData option to prevent this automatic processing. + * Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same + * key based on the value of the traditional setting (described below). + */ + data?: PlainObject | string; + /** + * A function to be used to handle the raw response data of XMLHttpRequest. This is a pre-filtering + * function to sanitize the response. You should return the sanitized data. The function accepts two + * arguments: The raw data returned from the server and the 'dataType' parameter. + */ + dataFilter?(data: string, type: string): any; + /** + * The type of data that you're expecting back from the server. If none is specified, jQuery will try + * to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON + * will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be + * returned as a string). The available types (and the result passed as the first argument to your + * success callback) are: + * + * "xml": Returns a XML document that can be processed via jQuery. + * + * "html": Returns HTML as plain text; included script tags are evaluated when inserted in the DOM. + * + * "script": Evaluates the response as JavaScript and returns it as plain text. Disables caching by + * appending a query string parameter, _=[TIMESTAMP], to the URL unless the cache option is set to + * true. Note: This will turn POSTs into GETs for remote-domain requests. + * + * "json": Evaluates the response as JSON and returns a JavaScript object. Cross-domain "json" requests + * are converted to "jsonp" unless the request includes jsonp: false in its request options. The JSON + * data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. As of + * jQuery 1.9, an empty response is also rejected; the server should return a response of null or {} + * instead. (See json.org for more information on proper JSON formatting.) + * + * "jsonp": Loads in a JSON block using JSONP. Adds an extra "?callback=?" to the end of your URL to + * specify the callback. Disables caching by appending a query string parameter, "_=[TIMESTAMP]", to + * the URL unless the cache option is set to true. + * + * "text": A plain text string. + * + * multiple, space-separated values: As of jQuery 1.5, jQuery can convert a dataType from what it + * received in the Content-Type header to what you require. For example, if you want a text response to + * be treated as XML, use "text xml" for the dataType. You can also make a JSONP request, have it + * received as text, and interpreted by jQuery as XML: "jsonp text xml". Similarly, a shorthand string + * such as "jsonp xml" will first attempt to convert from jsonp to xml, and, failing that, convert from + * jsonp to text, and then from text to xml. + */ + dataType?: 'xml' | 'html' | 'script' | 'json' | 'jsonp' | 'text' | string; + /** + * A function to be called if the request fails. The function receives three arguments: The jqXHR (in + * jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an + * optional exception object, if one occurred. Possible values for the second argument (besides null) + * are "timeout", "error", "abort", and "parsererror". When an HTTP error occurs, errorThrown receives + * the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error." As of jQuery + * 1.5, the error setting can accept an array of functions. Each function will be called in turn. Note: + * This handler is not called for cross-domain script and cross-domain JSONP requests. This is an Ajax Event. + */ + error?: TypeOrArray>; + /** + * Whether to trigger global Ajax event handlers for this request. The default is true. Set to false to + * prevent the global handlers like ajaxStart or ajaxStop from being triggered. This can be used to + * control various Ajax Events. + */ + global?: boolean; + /** + * An object of additional header key/value pairs to send along with requests using the XMLHttpRequest + * transport. The header X-Requested-With: XMLHttpRequest is always added, but its default + * XMLHttpRequest value can be changed here. Values in the headers setting can also be overwritten from + * within the beforeSend function. + */ + headers?: PlainObject; + /** + * Allow the request to be successful only if the response has changed since the last request. This is + * done by checking the Last-Modified header. Default value is false, ignoring the header. In jQuery + * 1.4 this technique also checks the 'etag' specified by the server to catch unmodified data. + */ + ifModified?: boolean; + /** + * Allow the current environment to be recognized as "local," (e.g. the filesystem), even if jQuery + * does not recognize it as such by default. The following protocols are currently recognized as local: + * file, *-extension, and widget. If the isLocal setting needs modification, it is recommended to do so + * once in the $.ajaxSetup() method. + */ + isLocal?: boolean; + /** + * Override the callback function name in a JSONP request. This value will be used instead of + * 'callback' in the 'callback=?' part of the query string in the url. So {jsonp:'onJSONPLoad'} would + * result in 'onJSONPLoad=?' passed to the server. As of jQuery 1.5, setting the jsonp option to false + * prevents jQuery from adding the "?callback" string to the URL or attempting to use "=?" for + * transformation. In this case, you should also explicitly set the jsonpCallback setting. For example, + * { jsonp: false, jsonpCallback: "callbackName" }. If you don't trust the target of your Ajax + * requests, consider setting the jsonp property to false for security reasons. + */ + jsonp?: string | false; + /** + * Specify the callback function name for a JSONP request. This value will be used instead of the + * random name automatically generated by jQuery. It is preferable to let jQuery generate a unique name + * as it'll make it easier to manage the requests and provide callbacks and error handling. You may + * want to specify the callback when you want to enable better browser caching of GET requests. As of + * jQuery 1.5, you can also use a function for this setting, in which case the value of jsonpCallback + * is set to the return value of that function. + */ + jsonpCallback?: string | ((this: TContext) => string); + /** + * The HTTP method to use for the request (e.g. "POST", "GET", "PUT"). + */ + method?: string; + /** + * A mime type to override the XHR mime type. + */ + mimeType?: string; + /** + * A password to be used with XMLHttpRequest in response to an HTTP access authentication request. + */ + password?: string; + /** + * By default, data passed in to the data option as an object (technically, anything other than a + * string) will be processed and transformed into a query string, fitting to the default content-type + * "application/x-www-form-urlencoded". If you want to send a DOMDocument, or other non-processed data, + * set this option to false. + */ + processData?: boolean; + /** + * Only applies when the "script" transport is used (e.g., cross-domain requests with "jsonp" or + * "script" dataType and "GET" type). Sets the charset attribute on the script tag used in the request. + * Used when the character set on the local page is not the same as the one on the remote script. + */ + scriptCharset?: string; + /** + * An object of numeric HTTP codes and functions to be called when the response has the corresponding + * code. + * + * If the request is successful, the status code functions take the same parameters as the success + * callback; if it results in an error (including 3xx redirect), they take the same parameters as the error callback. + */ + statusCode?: StatusCodeCallbacks; + /** + * A function to be called if the request succeeds. The function gets passed three arguments: The data + * returned from the server, formatted according to the dataType parameter or the dataFilter callback + * function, if specified; a string describing the status; and the jqXHR (in jQuery 1.4.x, + * XMLHttpRequest) object. As of jQuery 1.5, the success setting can accept an array of functions. Each + * function will be called in turn. This is an Ajax Event. + */ + success?: TypeOrArray>; + /** + * Set a timeout (in milliseconds) for the request. A value of 0 means there will be no timeout. This + * will override any global timeout set with $.ajaxSetup(). The timeout period starts at the point the + * $.ajax call is made; if several other requests are in progress and the browser has no connections + * available, it is possible for a request to time out before it can be sent. In jQuery 1.4.x and + * below, the XMLHttpRequest object will be in an invalid state if the request times out; accessing any + * object members may throw an exception. In Firefox 3.0+ only, script and JSONP requests cannot be + * cancelled by a timeout; the script will run even if it arrives after the timeout period. + */ + timeout?: number; + /** + * Set this to true if you wish to use the traditional style of param serialization. + */ + traditional?: boolean; + /** + * An alias for method. You should use type if you're using versions of jQuery prior to 1.9.0. + */ + type?: string; + /** + * A username to be used with XMLHttpRequest in response to an HTTP access authentication request. + */ + username?: string; + /** + * Callback for creating the XMLHttpRequest object. Defaults to the ActiveXObject when available (IE), + * the XMLHttpRequest otherwise. Override to provide your own implementation for XMLHttpRequest or + * enhancements to the factory. + */ + xhr?(): XMLHttpRequest; + /** + * An object of fieldName-fieldValue pairs to set on the native XHR object. + * + * In jQuery 1.5, the withCredentials property was not propagated to the native XHR and thus CORS + * requests requiring it would ignore this flag. For this reason, we recommend using jQuery 1.5.1+ + * should you require the use of it. + */ + xhrFields?: PlainObject; + } + + // Status codes not listed require type annotations when defining the callback + type StatusCodeCallbacks = { + // jQuery treats 2xx and 304 status codes as a success + 200?: SuccessCallback; + 201?: SuccessCallback; + 202?: SuccessCallback; + 203?: SuccessCallback; + 204?: SuccessCallback; + 205?: SuccessCallback; + 206?: SuccessCallback; + 207?: SuccessCallback; + 208?: SuccessCallback; + 209?: SuccessCallback; + 210?: SuccessCallback; + 211?: SuccessCallback; + 212?: SuccessCallback; + 213?: SuccessCallback; + 214?: SuccessCallback; + 215?: SuccessCallback; + 216?: SuccessCallback; + 217?: SuccessCallback; + 218?: SuccessCallback; + 219?: SuccessCallback; + 220?: SuccessCallback; + 221?: SuccessCallback; + 222?: SuccessCallback; + 223?: SuccessCallback; + 224?: SuccessCallback; + 225?: SuccessCallback; + 226?: SuccessCallback; + 227?: SuccessCallback; + 228?: SuccessCallback; + 229?: SuccessCallback; + 230?: SuccessCallback; + 231?: SuccessCallback; + 232?: SuccessCallback; + 233?: SuccessCallback; + 234?: SuccessCallback; + 235?: SuccessCallback; + 236?: SuccessCallback; + 237?: SuccessCallback; + 238?: SuccessCallback; + 239?: SuccessCallback; + 240?: SuccessCallback; + 241?: SuccessCallback; + 242?: SuccessCallback; + 243?: SuccessCallback; + 244?: SuccessCallback; + 245?: SuccessCallback; + 246?: SuccessCallback; + 247?: SuccessCallback; + 248?: SuccessCallback; + 249?: SuccessCallback; + 250?: SuccessCallback; + 251?: SuccessCallback; + 252?: SuccessCallback; + 253?: SuccessCallback; + 254?: SuccessCallback; + 255?: SuccessCallback; + 256?: SuccessCallback; + 257?: SuccessCallback; + 258?: SuccessCallback; + 259?: SuccessCallback; + 260?: SuccessCallback; + 261?: SuccessCallback; + 262?: SuccessCallback; + 263?: SuccessCallback; + 264?: SuccessCallback; + 265?: SuccessCallback; + 266?: SuccessCallback; + 267?: SuccessCallback; + 268?: SuccessCallback; + 269?: SuccessCallback; + 270?: SuccessCallback; + 271?: SuccessCallback; + 272?: SuccessCallback; + 273?: SuccessCallback; + 274?: SuccessCallback; + 275?: SuccessCallback; + 276?: SuccessCallback; + 277?: SuccessCallback; + 278?: SuccessCallback; + 279?: SuccessCallback; + 280?: SuccessCallback; + 281?: SuccessCallback; + 282?: SuccessCallback; + 283?: SuccessCallback; + 284?: SuccessCallback; + 285?: SuccessCallback; + 286?: SuccessCallback; + 287?: SuccessCallback; + 288?: SuccessCallback; + 289?: SuccessCallback; + 290?: SuccessCallback; + 291?: SuccessCallback; + 292?: SuccessCallback; + 293?: SuccessCallback; + 294?: SuccessCallback; + 295?: SuccessCallback; + 296?: SuccessCallback; + 297?: SuccessCallback; + 298?: SuccessCallback; + 299?: SuccessCallback; + 304?: SuccessCallback; + + // Standard 3xx, 4xx, and 5xx status codes that are considered an error + 300?: ErrorCallback; + 301?: ErrorCallback; + 302?: ErrorCallback; + 303?: ErrorCallback; + 305?: ErrorCallback; + 306?: ErrorCallback; + 307?: ErrorCallback; + 308?: ErrorCallback; + 400?: ErrorCallback; + 401?: ErrorCallback; + 402?: ErrorCallback; + 403?: ErrorCallback; + 404?: ErrorCallback; + 405?: ErrorCallback; + 406?: ErrorCallback; + 407?: ErrorCallback; + 408?: ErrorCallback; + 409?: ErrorCallback; + 410?: ErrorCallback; + 411?: ErrorCallback; + 412?: ErrorCallback; + 413?: ErrorCallback; + 414?: ErrorCallback; + 415?: ErrorCallback; + 416?: ErrorCallback; + 417?: ErrorCallback; + 418?: ErrorCallback; + 421?: ErrorCallback; + 422?: ErrorCallback; + 423?: ErrorCallback; + 424?: ErrorCallback; + 426?: ErrorCallback; + 428?: ErrorCallback; + 429?: ErrorCallback; + 431?: ErrorCallback; + 451?: ErrorCallback; + 500?: ErrorCallback; + 501?: ErrorCallback; + 502?: ErrorCallback; + 503?: ErrorCallback; + 504?: ErrorCallback; + 505?: ErrorCallback; + 506?: ErrorCallback; + 507?: ErrorCallback; + 508?: ErrorCallback; + 510?: ErrorCallback; + 511?: ErrorCallback; + } & { [index: number]: SuccessCallback | ErrorCallback; }; } interface Transport { @@ -3618,7 +3767,7 @@ declare namespace JQuery { interface jqXHR extends Pick { responseJSON: any; - statusCode(map: PlainObject | Ajax.ErrorCallback>): void; + statusCode(map: Ajax.StatusCodeCallbacks): void; /** * Add handlers to be called when the Deferred object is either resolved or rejected. @@ -3730,7 +3879,7 @@ declare namespace JQuery { } interface FailCallback { - (jqXHR: TResolve, textStatus: Ajax.ErrorTextStatus | null, errorThrown: string): void; + (jqXHR: TResolve, textStatus: Ajax.ErrorTextStatus, errorThrown: string): void; } interface AlwaysCallback { diff --git a/types/jquery/jquery-tests.ts b/types/jquery/jquery-tests.ts index c31352cb02..7a05a5630a 100644 --- a/types/jquery/jquery-tests.ts +++ b/types/jquery/jquery-tests.ts @@ -1277,7 +1277,9 @@ function JQuery() { // $ExpectType JQuery $('p').show(); } + } + function queue() { function clearQueue() { // $ExpectType JQuery $('p').clearQueue('myQueue'); @@ -1379,6 +1381,17 @@ function JQuery() { // $ExpectType JQuery $('p').stop(true); } + + function promise() { + // $ExpectType { description: string; } & Promise, any, any> + $('p').promise('myQueue', { description: 'desc' }); + + // $ExpectType { description: string; } & Promise, any, any> + $('p').promise({ description: 'desc' }); + + // $ExpectType Promise, any, any> + $('p').promise(); + } } function events() { @@ -2403,6 +2416,1057 @@ function JQuery() { }); } } + + function manipulation() { + function after() { + // $ExpectType JQuery + $('p').after('

', new Element(), new Text(), $('p'), [new Element(), new Text()]); + + // $ExpectType JQuery + $('p').after(function(index, html) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + html; + + return '

'; + }); + + // $ExpectType JQuery + $('p').after(function(index, html) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + html; + + return new Element(); + }); + + // $ExpectType JQuery + $('p').after(function(index, html) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + html; + + return new Text(); + }); + + // $ExpectType JQuery + $('p').after(function(index, html) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + html; + + return [new Element(), new Text()]; + }); + + // $ExpectType JQuery + $('p').after(function(index, html) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + html; + + return $('p'); + }); + } + + function append() { + // $ExpectType JQuery + $('p').append('

', new Element(), new Text(), $('p'), [new Element(), new Text()]); + + // $ExpectType JQuery + $('p').append(function(index, html) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + html; + + return '

'; + }); + + // $ExpectType JQuery + $('p').append(function(index, html) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + html; + + return new Element(); + }); + + // $ExpectType JQuery + $('p').append(function(index, html) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + html; + + return new Text(); + }); + + // $ExpectType JQuery + $('p').append(function(index, html) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + html; + + return [new Element(), new Text()]; + }); + + // $ExpectType JQuery + $('p').append(function(index, html) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + html; + + return $('p'); + }); + + // $ExpectType JQuery + $('p').append($.parseHTML('myTextNode ')); + } + + function before() { + // $ExpectType JQuery + $('p').before('

', new Element(), new Text(), $('p'), [new Element(), new Text()]); + + // $ExpectType JQuery + $('p').before(function(index, html) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + html; + + return '

'; + }); + + // $ExpectType JQuery + $('p').before(function(index, html) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + html; + + return new Element(); + }); + + // $ExpectType JQuery + $('p').before(function(index, html) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + html; + + return new Text(); + }); + + // $ExpectType JQuery + $('p').before(function(index, html) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + html; + + return [new Element(), new Text()]; + }); + + // $ExpectType JQuery + $('p').before(function(index, html) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + html; + + return $('p'); + }); + } + + function prepend() { + // $ExpectType JQuery + $('p').prepend('

', new Element(), new Text(), $('p'), [new Element(), new Text()]); + + // $ExpectType JQuery + $('p').prepend(function(index, html) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + html; + + return '

'; + }); + + // $ExpectType JQuery + $('p').prepend(function(index, html) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + html; + + return new Element(); + }); + + // $ExpectType JQuery + $('p').prepend(function(index, html) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + html; + + return new Text(); + }); + + // $ExpectType JQuery + $('p').prepend(function(index, html) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + html; + + return [new Element(), new Text()]; + }); + + // $ExpectType JQuery + $('p').prepend(function(index, html) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + html; + + return $('p'); + }); + } + + function appendTo() { + // $ExpectType JQuery + $('span').appendTo('p'); + + // $ExpectType JQuery + $('span').appendTo('

'); + + // $ExpectType JQuery + $('span').appendTo(new HTMLElement()); + + // $ExpectType JQuery + $('span').appendTo([new HTMLElement()]); + + // $ExpectType JQuery + $('span').appendTo($('p')); + } + + function insertAfter() { + // $ExpectType JQuery + $('span').insertAfter('p'); + + // $ExpectType JQuery + $('span').insertAfter('

'); + + // $ExpectType JQuery + $('span').insertAfter(new HTMLElement()); + + // $ExpectType JQuery + $('span').insertAfter([new HTMLElement()]); + + // $ExpectType JQuery + $('span').insertAfter($('p')); + } + + function insertBefore() { + // $ExpectType JQuery + $('span').insertBefore('p'); + + // $ExpectType JQuery + $('span').insertBefore('

'); + + // $ExpectType JQuery + $('span').insertBefore(new HTMLElement()); + + // $ExpectType JQuery + $('span').insertBefore([new HTMLElement()]); + + // $ExpectType JQuery + $('span').insertBefore($('p')); + } + + function prependTo() { + // $ExpectType JQuery + $('span').prependTo('p'); + + // $ExpectType JQuery + $('span').prependTo('

'); + + // $ExpectType JQuery + $('span').prependTo(new HTMLElement()); + + // $ExpectType JQuery + $('span').prependTo([new HTMLElement()]); + + // $ExpectType JQuery + $('span').prependTo($('p')); + } + + function clone() { + // $ExpectType JQuery + $('p').clone(true, true); + + // $ExpectType JQuery + $('p').clone(true); + + // $ExpectType JQuery + $('p').clone(); + } + + function detach() { + // $ExpectType JQuery + $('p').detach('span'); + + // $ExpectType JQuery + $('p').detach(); + } + + function empty() { + // $ExpectType JQuery + $('p').empty(); + } + + function remove() { + // $ExpectType JQuery + $('p').remove('span'); + + // $ExpectType JQuery + $('p').remove(); + } + + function replaceAll() { + // $ExpectType JQuery + $('p').replaceAll('span'); + + // $ExpectType JQuery + $('p').replaceAll($('span')); + + // $ExpectType JQuery + $('p').replaceAll(new HTMLElement()); + + // $ExpectType JQuery + $('p').replaceAll([new HTMLElement()]); + } + + function replaceWith() { + // $ExpectType JQuery + $('p').replaceWith(''); + + // $ExpectType JQuery + $('p').replaceWith($('span')); + + // $ExpectType JQuery + $('p').replaceWith(new HTMLElement()); + + // $ExpectType JQuery + $('p').replaceWith([new HTMLElement()]); + + // $ExpectType JQuery + $('p').replaceWith(function() { + // $ExpectType HTMLElement + this; + + return this; + }); + } + + function unwrap() { + // $ExpectType JQuery + $('p').unwrap('span'); + + // $ExpectType JQuery + $('p').unwrap(); + } + + function wrap() { + // $ExpectType JQuery + $('p').wrap('span'); + + // $ExpectType JQuery + $('p').wrap(''); + + // $ExpectType JQuery + $('p').wrap(new HTMLElement()); + + // $ExpectType JQuery + $('p').wrap($('span')); + + // $ExpectType JQuery + $('p').wrap(function(index) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + + return ''; + }); + + // $ExpectType JQuery + $('p').wrap(function(index) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + + return $('span'); + }); + } + + function wrapAll() { + // $ExpectType JQuery + $('p').wrapAll('span'); + + // $ExpectType JQuery + $('p').wrapAll(''); + + // $ExpectType JQuery + $('p').wrapAll(new HTMLElement()); + + // $ExpectType JQuery + $('p').wrapAll($('span')); + + // $ExpectType JQuery + $('p').wrapAll(function() { + // $ExpectType HTMLElement + this; + + return ''; + }); + + // $ExpectType JQuery + $('p').wrapAll(function() { + // $ExpectType HTMLElement + this; + + return $('span'); + }); + } + + function wrapInner() { + // $ExpectType JQuery + $('p').wrapInner('span'); + + // $ExpectType JQuery + $('p').wrapInner(''); + + // $ExpectType JQuery + $('p').wrapInner(new HTMLElement()); + + // $ExpectType JQuery + $('p').wrapInner($('span')); + + // $ExpectType JQuery + $('p').wrapInner(function(index) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + + return ''; + }); + + // $ExpectType JQuery + $('p').wrapInner(function(index) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + + return $('span'); + }); + + // $ExpectType JQuery + $('p').wrapInner(function(index) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + + return new HTMLElement(); + }); + } + + function html() { + // $ExpectType JQuery + $('p').html(''); + + // $ExpectType JQuery + $('p').html(function(index, oldhtml) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + oldhtml; + + return oldhtml; + }); + + // $ExpectType string + $('p').html(); + } + + function text() { + // $ExpectType JQuery + $('p').text('myText'); + + // $ExpectType JQuery + $('p').text(4); + + // $ExpectType JQuery + $('p').text(true); + + // $ExpectType JQuery + $('p').text(function(index, text) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + text; + + return 'myText'; + }); + + // $ExpectType JQuery + $('p').text(function(index, text) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + text; + + return 3; + }); + + // $ExpectType JQuery + $('p').text(function(index, text) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + text; + + return false; + }); + + // $ExpectType string + $('p').text(); + } + + function val() { + // $ExpectType JQuery + $('p').val('myVal'); + + // $ExpectType JQuery + $('p').val(5); + + // $ExpectType JQuery + $('p').val(['myVal']); + + // $ExpectType JQuery + $('p').val(function(index, value) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + value; + + return 'myVal'; + }); + + // $ExpectType string | number | string[] | undefined + $('p').val(); + } + } + + function traversing() { + function add() { + // $ExpectType JQuery + $('p').add('span', new HTMLElement()); + + // $ExpectType JQuery + $('p').add('span'); + + // $ExpectType JQuery + $('p').add(new HTMLElement()); + + // $ExpectType JQuery + $('p').add([new HTMLElement()]); + + // $ExpectType JQuery + $('p').add(''); + + // $ExpectType JQuery + $('p').add($('span')); + } + + function closest() { + // $ExpectType JQuery + $('p').closest('span', new HTMLElement()); + + // $ExpectType JQuery + $('p').closest('span'); + + // $ExpectType JQuery + $('p').closest(new HTMLElement()); + + // $ExpectType JQuery + $('p').closest($('span')); + } + + function find() { + // $ExpectType JQuery + $('p').find('span'); + + // $ExpectType JQuery + $('p').find(new HTMLElement()); + + // $ExpectType JQuery + $('p').find($('span')); + } + + function addBack() { + // $ExpectType JQuery + $('p').addBack('span'); + + // $ExpectType JQuery + $('p').addBack(); + } + + function children() { + // $ExpectType JQuery + $('p').children('span'); + + // $ExpectType JQuery + $('p').children(); + } + + function siblings() { + // $ExpectType JQuery + $('p').siblings('span'); + + // $ExpectType JQuery + $('p').siblings(); + } + + function contents() { + // $ExpectType JQuery + $('p').contents(); + } + + function end() { + // $ExpectType JQuery + $('p').end(); + } + + function first() { + // $ExpectType JQuery + $('p').first(); + } + + function last() { + // $ExpectType JQuery + $('p').last(); + } + + function offsetParent() { + // $ExpectType JQuery + $('p').offsetParent(); + } + + function filter() { + // $ExpectType JQuery + $('p').filter('span'); + + // $ExpectType JQuery + $('p').filter(new HTMLElement()); + + // $ExpectType JQuery + $('p').filter([new HTMLElement()]); + + // $ExpectType JQuery + $('p').filter($('span')); + + // $ExpectType JQuery + $('p').filter(function(index, element) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType HTMLElement + element; + + return false; + }); + } + + function not() { + // $ExpectType JQuery + $('p').not('span'); + + // $ExpectType JQuery + $('p').not(new HTMLElement()); + + // $ExpectType JQuery + $('p').not([new HTMLElement()]); + + // $ExpectType JQuery + $('p').not($('span')); + + // $ExpectType JQuery + $('p').not(function(index, element) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType HTMLElement + element; + + return false; + }); + } + + function is() { + // $ExpectType boolean + $('p').is('span'); + + // $ExpectType boolean + $('p').is(new HTMLElement()); + + // $ExpectType boolean + $('p').is([new HTMLElement()]); + + // $ExpectType boolean + $('p').is($('span')); + + // $ExpectType boolean + $('p').is(function(index, element) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType HTMLElement + element; + + return false; + }); + } + + function next() { + // $ExpectType JQuery + $('p').next('span'); + + // $ExpectType JQuery + $('p').next(); + } + + function nextAll() { + // $ExpectType JQuery + $('p').nextAll('span'); + + // $ExpectType JQuery + $('p').nextAll(); + } + + function nextUntil() { + // $ExpectType JQuery + $('p').nextUntil('span', 'span'); + + // $ExpectType JQuery + $('p').nextUntil(new HTMLElement(), 'span'); + + // $ExpectType JQuery + $('p').nextUntil($('span'), 'span'); + + // $ExpectType JQuery + $('p').nextUntil('span'); + + // $ExpectType JQuery + $('p').nextUntil(new HTMLElement()); + + // $ExpectType JQuery + $('p').nextUntil($('span')); + + // $ExpectType JQuery + $('p').nextUntil(); + } + + function prev() { + // $ExpectType JQuery + $('p').prev('span'); + + // $ExpectType JQuery + $('p').prev(); + } + + function prevAll() { + // $ExpectType JQuery + $('p').prevAll('span'); + + // $ExpectType JQuery + $('p').prevAll(); + } + + function prevUntil() { + // $ExpectType JQuery + $('p').prevUntil('span', 'span'); + + // $ExpectType JQuery + $('p').prevUntil(new HTMLElement(), 'span'); + + // $ExpectType JQuery + $('p').prevUntil($('span'), 'span'); + + // $ExpectType JQuery + $('p').prevUntil('span'); + + // $ExpectType JQuery + $('p').prevUntil(new HTMLElement()); + + // $ExpectType JQuery + $('p').prevUntil($('span')); + + // $ExpectType JQuery + $('p').prevUntil(); + } + + function parent() { + // $ExpectType JQuery + $('p').parent('span'); + + // $ExpectType JQuery + $('p').parent(); + } + + function parents() { + // $ExpectType JQuery + $('p').parents('span'); + + // $ExpectType JQuery + $('p').parents(); + } + + function parentsUntil() { + // $ExpectType JQuery + $('p').parentsUntil('span', 'span'); + + // $ExpectType JQuery + $('p').parentsUntil(new HTMLElement(), 'span'); + + // $ExpectType JQuery + $('p').parentsUntil($('span'), 'span'); + + // $ExpectType JQuery + $('p').parentsUntil('span'); + + // $ExpectType JQuery + $('p').parentsUntil(new HTMLElement()); + + // $ExpectType JQuery + $('p').parentsUntil($('span')); + + // $ExpectType JQuery + $('p').parentsUntil(); + } + + function eq() { + // $ExpectType JQuery + $('p').eq(0); + } + + function has() { + // $ExpectType JQuery + $('p').has('span'); + + // $ExpectType JQuery + $('p').has(new HTMLElement()); + } + + function map() { + // $ExpectType JQuery + $('p').map(function(index, domElement) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType HTMLElement + domElement; + + return 'myVal'; + }); + + // $ExpectType JQuery + $('p').map(function(index, domElement) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType HTMLElement + domElement; + + return ['myVal1', 'myVal2']; + }); + + // $ExpectType JQuery + $('p').map(function(index, domElement) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType HTMLElement + domElement; + + return null; + }); + + // $ExpectType JQuery + $('p').map(function(index, domElement) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType HTMLElement + domElement; + + return undefined; + }); + } + + function slice() { + // $ExpectType JQuery + $('p').slice(0, 10); + + // $ExpectType JQuery + $('p').slice(0); + } + + function pushStack() { + // $ExpectType JQuery + $('p').pushStack([new HTMLElement()], 'name', ['arg']); + + // $ExpectType JQuery + $('p').pushStack([new HTMLElement()]); + } + } + + function misc() { + function serialize() { + // $ExpectType string + $('p').serialize(); + } + + function serializeArray() { + // $ExpectType NameValuePair[] + $('p').serializeArray(); + } + + function each() { + // $ExpectType JQuery + $('p').each(function(index, element) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType HTMLElement + element; + }); + + // $ExpectType JQuery + $('p').each(function(index, element) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType HTMLElement + element; + + return false; + }); + } + + function extend() { + // $ExpectType JQuery + $.fn.extend({ myPlugin: {} }); + } + + function get() { + // $ExpectType HTMLElement + $('p').get(0); + + // $ExpectType HTMLElement[] + $('p').get(); + } + + function index() { + // $ExpectType number + $('p').index('span'); + + // $ExpectType number + $('p').index(new HTMLElement()); + + // $ExpectType number + $('p').index($('span')); + + // $ExpectType number + $('p').index(); + } + + function toArray() { + // $ExpectType HTMLElement[] + $('p').toArray(); + } + } } function JQueryStatic() { @@ -2414,18 +3478,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() { @@ -2435,26 +3546,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; } } } @@ -2462,7 +4015,8 @@ function JQueryStatic() { function isNumeric() { function type_guard(obj: boolean) { if ($.isNumeric(obj)) { - obj.toFixed(); + // $ExpectType (true & number) | (false & number) + obj; } } } @@ -2470,7 +4024,8 @@ function JQueryStatic() { function isPlainObject() { function type_guard(obj: object) { if ($.isPlainObject(obj)) { - obj['key'] = true; + // $ExpectType PlainObject + obj; } } } @@ -2478,37 +4033,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() { @@ -2516,6 +4273,260 @@ 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 AjaxSettings() { + $.ajax({ + accepts: { + mycustomtype: 'application/x-some-custom-type' + }, + async: true, + beforeSend(jqXHR, settings) { + // $ExpectType any + this; + // $ExpectType jqXHR + jqXHR; + // $ExpectType AjaxSettingsBase + settings; + }, + cache: false, + complete(jqXHR, textStatus) { + // $ExpectType any + this; + // $ExpectType jqXHR + jqXHR; + // $ExpectType TextStatus + textStatus; + }, + contents: { + mycustomtype: /mycustomtype/ + }, + contentType: 'application/x-some-custom-type', + converters: { + 'text mycustomtype': true, + 'mycustomtype json': (result) => { + // $ExpectType any + result; + + return result; + } + }, + crossDomain: false, + data: { + myData: 'myData' + }, + dataFilter(data, type) { + // $ExpectType string + data; + // $ExpectType string + type; + + return 'filtered'; + }, + dataType: 'mycustomtype', + error(jqXHR, textStatus, errorThrown) { + // $ExpectType any + this; + // $ExpectType jqXHR + jqXHR; + // $ExpectType ErrorTextStatus + textStatus; + // $ExpectType string + errorThrown; + }, + global: true, + headers: { + 'X-Requested-With': 'XMLHttpRequest' + }, + ifModified: false, + isLocal: true, + jsonp: 'callback', + jsonpCallback: 'callback', + method: 'PUT', + mimeType: 'mimeType', + password: 'hunter2', + processData: false, + scriptCharset: 'scriptCharset', + statusCode: { + 200(data, textStatus, jqXHR) { + // $ExpectType any + this; + // $ExpectType any + data; + // $ExpectType SuccessTextStatus + textStatus; + // $ExpectType jqXHR + jqXHR; + }, + 404(jqXHR, textStatus, errorThrown) { + // $ExpectType any + this; + // $ExpectType jqXHR + jqXHR; + // $ExpectType ErrorTextStatus + textStatus; + // $ExpectType string + errorThrown; + } + }, + success(data, textStatus, jqXHR) { + // $ExpectType any + this; + // $ExpectType any + data; + // $ExpectType SuccessTextStatus + textStatus; + // $ExpectType jqXHR + jqXHR; + }, + timeout: 10, + traditional: true, + username: 'username', + xhr() { + return new XMLHttpRequest(); + }, + xhrFields: { + withCredentials: true + } + }); + + $.ajax({ + beforeSend(jqXHR, settings) { + // $ExpectType any + this; + // $ExpectType jqXHR + jqXHR; + // $ExpectType AjaxSettingsBase + settings; + + return false; + }, + complete: [function(jqXHR, textStatus) { + // $ExpectType any + this; + // $ExpectType jqXHR + jqXHR; + // $ExpectType TextStatus + textStatus; + }], + contentType: false, + data: 'myData', + error: [function(jqXHR, textStatus, errorThrown) { + // $ExpectType any + this; + // $ExpectType jqXHR + jqXHR; + // $ExpectType ErrorTextStatus + textStatus; + // $ExpectType string + errorThrown; + }], + jsonp: false, + jsonpCallback() { + // $ExpectType any + this; + + return 'callback'; + }, + success: [function(data, textStatus, jqXHR) { + // $ExpectType any + this; + // $ExpectType any + data; + // $ExpectType SuccessTextStatus + textStatus; + // $ExpectType jqXHR + jqXHR; + }] + }); } function EffectsOptions() { @@ -2629,9 +4640,7 @@ function jqXHR() { $.ajax('/echo').fail((jqXHR, textStatus, errorThrown) => { // $ExpectType jqXHR jqXHR; - // This test is flaky - // Should be 'ErrorTextStatus | null' and should be able to handle it out of order - // $ExpectType "timeout" | "error" | "abort" | "parsererror" | null + // $ExpectType ErrorTextStatus textStatus; // $ExpectType string errorThrown; diff --git a/types/jquery/test/example-tests.ts b/types/jquery/test/example-tests.ts index 961e239192..404aba2013 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() { @@ -2907,7 +2907,7 @@ function examples() { function jQuery_parse_html_0() { var $log = $('#log'), str = 'hello, my name is jQuery.', - html = $.parseHTML(str) as HTMLElement[], + html = $.parseHTML(str), nodeNames: string[] = []; // Append the parsed HTML @@ -4056,7 +4056,7 @@ function examples() { function promise_0() { var div = $('
'); - div.promise().done(function(this: JQuery, arg1: JQuery) { + div.promise().done(function(this: typeof div, arg1) { // Will fire right away and alert "true" alert(this === div && arg1 === div); });