diff --git a/types/jquery/index.d.ts b/types/jquery/index.d.ts index 598d71bc60..cea056ef60 100644 --- a/types/jquery/index.d.ts +++ b/types/jquery/index.d.ts @@ -25,14 +25,14 @@ // TypeScript Version: 2.3 declare module 'jquery' { - function factory(window: Window): JQueryStatic; + function factory(window: Window): JQueryStatic & JQuery; const factoryOrJQuery: typeof factory & JQueryStatic; export = factoryOrJQuery; } declare module 'jquery/dist/jquery.slim' { - function factory(window: Window): JQueryStatic; + function factory(window: Window): JQueryStatic & JQuery; const factoryOrJQuery: typeof factory & JQueryStatic; export = factoryOrJQuery; @@ -129,7 +129,7 @@ interface JQuery { * @see {@link https://api.jquery.com/ajaxComplete/} * @since 1.0 */ - ajaxComplete(handler: (this: Document, event: JQuery.Event, jqXHR: JQuery.jqXHR, ajaxOptions: JQuery.AjaxSettings) => void | false): this; + ajaxComplete(handler: (this: Document, event: JQuery.Event, jqXHR: JQuery.jqXHR, ajaxOptions: JQuery.AjaxSettings) => void | false): this; /** * Register a handler to be called when Ajax requests complete with an error. This is an Ajax Event. * @@ -137,7 +137,7 @@ interface JQuery { * @see {@link https://api.jquery.com/ajaxError/} * @since 1.0 */ - ajaxError(handler: (this: Document, event: JQuery.Event, jqXHR: JQuery.jqXHR, ajaxSettings: JQuery.AjaxSettings, thrownError: string) => void | false): this; + ajaxError(handler: (this: Document, event: JQuery.Event, jqXHR: JQuery.jqXHR, ajaxSettings: JQuery.AjaxSettings, thrownError: string) => void | false): this; /** * Attach a function to be executed before an Ajax request is sent. This is an Ajax Event. * @@ -145,7 +145,7 @@ interface JQuery { * @see {@link https://api.jquery.com/ajaxSend/} * @since 1.0 */ - ajaxSend(handler: (this: Document, event: JQuery.Event, jqXHR: JQuery.jqXHR, ajaxOptions: JQuery.AjaxSettings) => void | false): this; + ajaxSend(handler: (this: Document, event: JQuery.Event, jqXHR: JQuery.jqXHR, ajaxOptions: JQuery.AjaxSettings) => void | false): this; /** * Register a handler to be called when the first Ajax request begins. This is an Ajax Event. * @@ -169,7 +169,7 @@ interface JQuery { * @see {@link https://api.jquery.com/ajaxSuccess/} * @since 1.0 */ - ajaxSuccess(handler: (this: Document, event: JQuery.Event, jqXHR: JQuery.jqXHR, ajaxOptions: JQuery.AjaxSettings, data: JQuery.PlainObject) => void | false): this; + ajaxSuccess(handler: (this: Document, event: JQuery.Event, jqXHR: JQuery.jqXHR, ajaxOptions: JQuery.AjaxSettings, data: JQuery.PlainObject) => void | false): this; /** * Perform a custom animation of a set of CSS properties. * @@ -201,15 +201,22 @@ interface JQuery { * Perform a custom animation of a set of CSS properties. * * @param properties An object of CSS properties and values that the animation will move toward. - * @param duration_easing_complete_options 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. - * A map of additional options to pass to the method. + * @param options A map of additional options to pass to the method. * @see {@link https://api.jquery.com/animate/} * @since 1.0 */ animate(properties: JQuery.PlainObject, - duration_easing_complete_options?: JQuery.Duration | string | ((this: TElement) => void) | JQuery.EffectsOptions): this; + options: JQuery.EffectsOptions): this; + /** + * Perform a custom animation of a set of CSS properties. + * + * @param properties An object of CSS properties and values that the animation will move toward. + * @param complete A function to call once the animation is complete, called once per matched element. + * @see {@link https://api.jquery.com/animate/} + * @since 1.0 + */ + animate(properties: JQuery.PlainObject, + complete?: (this: TElement) => void): this; /** * Insert content, specified by the parameter, to the end of each element in the set of matched elements. * @@ -516,8 +523,7 @@ interface JQuery { * @see {@link https://api.jquery.com/data/} * @since 1.2.3 */ - // tslint:disable-next-line:unified-signatures - data(key: string, undefined: undefined): any; + data(key: string, undefined: undefined): any; // tslint:disable-line:unified-signatures /** * Store arbitrary data associated with the matched elements. * @@ -693,7 +699,7 @@ interface JQuery { * @since 1.0 * @since 1.4.3 */ - fadeIn(duration_easing: JQuery.Duration | string, complete?: (this: TElement) => void): this; + fadeIn(duration_easing: JQuery.Duration | string, complete: (this: TElement) => void): this; /** * Display the matched elements by fading them to opaque. * @@ -726,7 +732,7 @@ interface JQuery { * @since 1.0 * @since 1.4.3 */ - fadeOut(duration_easing: JQuery.Duration | string, complete?: (this: TElement) => void): this; + fadeOut(duration_easing: JQuery.Duration | string, complete: (this: TElement) => void): this; /** * Hide the matched elements by fading them to transparent. * @@ -780,7 +786,7 @@ interface JQuery { * @since 1.0 * @since 1.4.3 */ - fadeToggle(duration_easing: JQuery.Duration | string, complete?: (this: TElement) => void): this; + fadeToggle(duration_easing: JQuery.Duration | string, complete: (this: TElement) => void): this; /** * Display or hide the matched elements by animating their opacity. * @@ -933,7 +939,7 @@ interface JQuery { * @see {@link https://api.jquery.com/height/} * @since 1.0 */ - height(): number; + height(): number | undefined; /** * Hide the matched elements. * @@ -1025,7 +1031,7 @@ interface JQuery { * @see {@link https://api.jquery.com/innerHeight/} * @since 1.2.6 */ - innerHeight(): number; + innerHeight(): number | undefined; /** * Set the CSS inner width of each element in the set of matched elements. * @@ -1045,7 +1051,7 @@ interface JQuery { * @see {@link https://api.jquery.com/innerWidth/} * @since 1.2.6 */ - innerWidth(): number; + innerWidth(): number | undefined; /** * Insert every element in the set of matched elements after the target. * @@ -1148,7 +1154,7 @@ interface JQuery { */ load(url: string, data: string | JQuery.PlainObject, - complete: (this: TElement, responseText: string, textStatus: string, jqXHR: JQuery.jqXHR) => void): this; + complete: (this: TElement, responseText: string, textStatus: JQuery.Ajax.TextStatus, jqXHR: JQuery.jqXHR) => void): this; /** * Load data from the server and place the returned HTML into the matched element. * @@ -1159,7 +1165,7 @@ interface JQuery { * @since 1.0 */ load(url: string, - complete_data?: ((this: TElement, responseText: string, textStatus: string, jqXHR: JQuery.jqXHR) => void) | string | JQuery.PlainObject): this; + complete_data?: ((this: TElement, responseText: string, textStatus: JQuery.Ajax.TextStatus, jqXHR: JQuery.jqXHR) => void) | string | JQuery.PlainObject): this; /** * Pass each element in the current matched set through a function, producing a new jQuery object * containing the return values. @@ -1535,7 +1541,7 @@ interface JQuery { * @see {@link https://api.jquery.com/outerHeight/} * @since 1.2.6 */ - outerHeight(includeMargin?: boolean): number; + outerHeight(includeMargin?: boolean): number | undefined; /** * Set the CSS outer width of each element in the set of matched elements. * @@ -1555,7 +1561,7 @@ interface JQuery { * @see {@link https://api.jquery.com/outerWidth/} * @since 1.2.6 */ - outerWidth(includeMargin?: boolean): number; + outerWidth(includeMargin?: boolean): number | undefined; /** * Get the parent of each element in the current set of matched elements, optionally filtered by a selector. * @@ -1610,7 +1616,7 @@ interface JQuery { * @see {@link https://api.jquery.com/prepend/} * @since 1.4 */ - prepend(fn: (this: TElement, elementOfArray: 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. * @@ -1619,7 +1625,7 @@ interface JQuery { * @see {@link https://api.jquery.com/prependTo/} * @since 1.0 */ - prependTo(target: JQuery.Selector | JQuery.htmlString | JQuery.TypeOrArray | JQuery): this; + prependTo(target: JQuery.Selector | JQuery.htmlString | JQuery.TypeOrArray | JQuery): this; /** * Get the immediately preceding sibling of each element in the set of matched elements. If a selector * is provided, it retrieves the previous sibling only if it matches that selector. @@ -1695,8 +1701,7 @@ interface JQuery { * @see {@link https://api.jquery.com/prop/} * @since 1.6 */ - // tslint:disable-next-line:unified-signatures - prop(propertyName: string, value: any): this; + prop(propertyName: string, value: any): this; // tslint:disable-line:unified-signatures /** * Set one or more properties for the set of matched elements. * @@ -1996,7 +2001,7 @@ interface JQuery { * @since 1.0 * @since 1.4.3 */ - slideDown(duration_easing: JQuery.Duration | string, complete?: (this: TElement) => void): this; + slideDown(duration_easing: JQuery.Duration | string, complete: (this: TElement) => void): this; /** * Display the matched elements with a sliding motion. * @@ -2029,7 +2034,7 @@ interface JQuery { * @since 1.0 * @since 1.4.3 */ - slideToggle(duration_easing: JQuery.Duration | string, complete?: (this: TElement) => void): this; + slideToggle(duration_easing: JQuery.Duration | string, complete: (this: TElement) => void): this; /** * Display or hide the matched elements with a sliding motion. * @@ -2062,7 +2067,7 @@ interface JQuery { * @since 1.0 * @since 1.4.3 */ - slideUp(duration_easing: JQuery.Duration | string, complete?: (this: TElement) => void): this; + slideUp(duration_easing: JQuery.Duration | string, complete: (this: TElement) => void): this; /** * Hide the matched elements with a sliding motion. * @@ -2155,17 +2160,19 @@ interface JQuery { * @see {@link https://api.jquery.com/toggle/} * @since 1.0 */ - toggle(duration: JQuery.Duration, complete?: (this: TElement) => void): this; + toggle(duration: JQuery.Duration, complete: (this: TElement) => void): this; /** * Display or hide the matched elements. * - * @param options A map of additional options to pass to the method. - * Use true to show the element or false to hide it. + * @param duration_complete_options_display 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. + * A map of additional options to pass to the method. + * Use true to show the element or false to hide it. * @see {@link https://api.jquery.com/toggle/} * @since 1.0 * @since 1.3 */ - toggle(options?: JQuery.EffectsOptions | boolean): this; + toggle(duration_complete_options_display?: JQuery.Duration | ((this: TElement) => void) | JQuery.EffectsOptions | boolean): this; /** * Add or remove one or more classes from each element in the set of matched elements, depending on * either the class's presence or the value of the state argument. @@ -2179,10 +2186,11 @@ interface JQuery { * @since 1.3 * @since 1.4 */ - toggleClass(className: string | ((this: TElement, index: number, className: string, state: boolean) => string), - state?: boolean): this; + toggleClass(className: string | ((this: TElement, index: number, className: string, state: TState) => string), + state?: TState): this; /** - * + * Add or remove one or more classes from each element in the set of matched elements, depending on + * either the class's presence or the value of the state argument. * * @param state A boolean value to determine whether the class should be added or removed. * @see {@link https://api.jquery.com/toggleClass/} @@ -2316,7 +2324,7 @@ interface JQuery { * @see {@link https://api.jquery.com/width/} * @since 1.0 */ - width(): number; + width(): number | undefined; /** * Wrap an HTML structure around each element in the set of matched elements. * @@ -2356,7 +2364,7 @@ interface JQuery { * @since 1.2 * @since 1.4 */ - wrapInner(wrappingElement: JQuery.htmlString | JQuery.Selector | JQuery | Element | ((this: TElement, index: number) => string)): this; + wrapInner(wrappingElement: JQuery.Selector | JQuery.htmlString | Element | JQuery | ((this: TElement, index: number) => string | Element | JQuery)): this; } interface JQuery extends ArrayLike, Iterable { } @@ -2553,8 +2561,7 @@ interface JQueryStatic { * @see {@link https://api.jquery.com/jQuery.data/} * @since 1.2.3 */ - // tslint:disable-next-line:unified-signatures - data(element: Element, key: string, undefined: undefined): any; + data(element: Element, key: string, undefined: undefined): any; // tslint:disable-line:unified-signatures /** * Store arbitrary data associated with the specified element. Returns the value that was set. * @@ -4043,6 +4050,7 @@ declare namespace JQuery { // region Effects type Duration = number | 'fast' | 'slow'; + // TODO: Is the first element always a string or is that specific to the 'fx' queue? type Queue = { 0: string; } & Array>; interface QueueFunction { @@ -4094,7 +4102,7 @@ declare namespace JQuery { * An object containing one or more of the CSS properties defined by the properties argument and their * corresponding easing functions. */ - specialEasing?: PlainObject; + specialEasing?: PlainObject; /** * A function to call when the animation on an element begins. */ diff --git a/types/jquery/jquery-tests.ts b/types/jquery/jquery-tests.ts index 09468a1c55..0e2e7ee350 100644 --- a/types/jquery/jquery-tests.ts +++ b/types/jquery/jquery-tests.ts @@ -9,6 +9,1373 @@ function JQuery() { $('div')[0] === new HTMLElement(); } + function ajax() { + function ajaxComplete() { + // $ExpectType JQuery + $(document).ajaxComplete(function(event, jqXHR, ajaxOptions) { + // $ExpectType Document + this; + // $ExpectType Event + event; + // $ExpectType jqXHR + jqXHR; + // $ExpectType AjaxSettings + ajaxOptions; + + return false; + }); + } + + function ajaxError() { + // $ExpectType JQuery + $(document).ajaxError(function(event, jqXHR, ajaxSettings, thrownError) { + // $ExpectType Document + this; + // $ExpectType Event + event; + // $ExpectType jqXHR + jqXHR; + // $ExpectType AjaxSettings + ajaxSettings; + // $ExpectType string + thrownError; + + return false; + }); + } + + function ajaxSend() { + // $ExpectType JQuery + $(document).ajaxSend(function(event, jqXHR, ajaxOptions) { + // $ExpectType Document + this; + // $ExpectType Event + event; + // $ExpectType jqXHR + jqXHR; + // $ExpectType AjaxSettings + ajaxOptions; + + return false; + }); + } + + function ajaxStart() { + // $ExpectType JQuery + $(document).ajaxStart(function() { + // $ExpectType Document + this; + + return false; + }); + } + + function ajaxStop() { + // $ExpectType JQuery + $(document).ajaxStop(function() { + // $ExpectType Document + this; + + return false; + }); + } + + function ajaxSuccess() { + // $ExpectType JQuery + $(document).ajaxSuccess(function(event, jqXHR, ajaxOptions, data) { + // $ExpectType Document + this; + // $ExpectType Event + event; + // $ExpectType jqXHR + jqXHR; + // $ExpectType AjaxSettings + ajaxOptions; + // $ExpectType PlainObject + data; + + return false; + }); + } + + function load() { + // $ExpectType JQuery + $('#result').load('/echo/html/', 'data', function(responseText, textStatus, jqXHR) { + // $ExpectType HTMLElement + this; + // $ExpectType string + responseText; + // $ExpectType TextStatus + textStatus; + // $ExpectType jqXHR + jqXHR; + }); + + // $ExpectType JQuery + $('#result').load('/echo/html/', { data: 'data' }, function(responseText, textStatus, jqXHR) { + // $ExpectType HTMLElement + this; + // $ExpectType string + responseText; + // $ExpectType TextStatus + textStatus; + // $ExpectType jqXHR + jqXHR; + }); + + // $ExpectType JQuery + $('#result').load('/echo/html/', function(responseText, textStatus, jqXHR) { + // $ExpectType HTMLElement + this; + // $ExpectType string + responseText; + // $ExpectType TextStatus + textStatus; + // $ExpectType jqXHR + jqXHR; + }); + + // $ExpectType JQuery + $('#result').load('/echo/html/', 'data'); + + // $ExpectType JQuery + $('#result').load('/echo/html/', { data: 'data' }); + } + } + + function attributes() { + function attr() { + // $ExpectType JQuery + $('#greatphoto').attr('alt', 'Beijing Brush Seller'); + + // $ExpectType JQuery + $('#greatphoto').attr('width', 200); + + // $ExpectType JQuery + $('#greatphoto').attr('title', null); + + // $ExpectType JQuery + $('#greatphoto').attr('alt', function(index, attr) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + attr; + + return 'Beijing Brush Seller'; + }); + + // $ExpectType JQuery + $('#greatphoto').attr('width', function(index, attr) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + attr; + + return 200; + }); + + // $ExpectType JQuery + $('#greatphoto').attr('title', function(index, attr) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + attr; + }); + + // $ExpectType JQuery + $('#greatphoto').attr('title', function(index, attr) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + attr; + + return undefined; + }); + + // $ExpectType JQuery + $('img').attr({ + src: '/resources/hat.gif', + title: 'jQuery', + alt: 'jQuery Logo' + }); + + // $ExpectType string | undefined + $('img').attr('src'); + } + + function removeAttr() { + // $ExpectType JQuery + $('#greatphoto').removeAttr('alt'); + } + } + + function properties() { + function prop() { + // $ExpectType JQuery + $('p').prop('myProp', function(index, oldPropertyValue) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType any + oldPropertyValue; + + return {}; + }); + + // $ExpectType JQuery + $('p').prop('myProp', {}); + + // $ExpectType JQuery + $('input[type=\'checkbox\']').prop({ + myProp: true + }); + + // $ExpectType any + $('input[type=\'checkbox\']').prop('myProp'); + } + + function removeProp() { + // $ExpectType JQuery + $('p').removeProp('luggageCode'); + } + } + + function css() { + // TODO: .css() getters can return 'undefined' for properties that don't exist. Consider changing the return types to reflect this after adding specialized signatures. + function css() { + // $ExpectType JQuery + $('p').css('cssProp', 'value'); + + // $ExpectType JQuery + $('p').css('cssProp', 20); + + // $ExpectType JQuery + $('p').css('cssProp', function(index, value) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + value; + + return 'value'; + }); + + // $ExpectType JQuery + $('p').css('cssProp', function(index, value) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + value; + + return 20; + }); + + // $ExpectType JQuery + $('p').css('cssProp', function(index, value) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + value; + }); + + // $ExpectType JQuery + $('p').css('cssProp', function(index, value) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + value; + + return undefined; + }); + + // $ExpectType JQuery + $('p').css({ + myProp1: 'value', + myProp2: 20, + myProp3(index, value) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + value; + + return 'value'; + }, + myProp4(index, value) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + value; + + return 20; + }, + myProp5(index, value) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + value; + }, + myProp6(index, value) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + value; + + return undefined; + } + }); + + // $ExpectType string + $('p').css('myProp'); + + // $ExpectType PlainObject + $('p').css([ + 'myProp1', + 'myProp2' + ]); + } + + function height() { + // $ExpectType JQuery + $('p').height('200px'); + + // $ExpectType JQuery + $('p').height(400); + + // $ExpectType JQuery + $('p').height(function(index, height) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType number + height; + + return '200px'; + }); + + // $ExpectType JQuery + $('p').height(function(index, height) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType number + height; + + return 400; + }); + + // $ExpectType number | undefined + $('p').height(); + } + + function innerHeight() { + // $ExpectType JQuery + $('p').innerHeight('200px'); + + // $ExpectType JQuery + $('p').innerHeight(400); + + // $ExpectType JQuery + $('p').innerHeight(function(index, height) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType number + height; + + return '200px'; + }); + + // $ExpectType JQuery + $('p').innerHeight(function(index, height) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType number + height; + + return 400; + }); + + // $ExpectType number | undefined + $('p').innerHeight(); + } + + function outerHeight() { + // $ExpectType JQuery + $('p').outerHeight('200px'); + + // $ExpectType JQuery + $('p').outerHeight(400); + + // $ExpectType JQuery + $('p').outerHeight(function(index, height) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType number + height; + + return '200px'; + }); + + // $ExpectType JQuery + $('p').outerHeight(function(index, height) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType number + height; + + return 400; + }); + + // $ExpectType number | undefined + $('p').outerHeight(); + + // $ExpectType number | undefined + $('p').outerHeight(true); + } + + function width() { + // $ExpectType JQuery + $('p').width('200px'); + + // $ExpectType JQuery + $('p').width(400); + + // $ExpectType JQuery + $('p').width(function(index, width) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType number + width; + + return '200px'; + }); + + // $ExpectType JQuery + $('p').width(function(index, width) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType number + width; + + return 400; + }); + + // $ExpectType number | undefined + $('p').width(); + } + + function innerWidth() { + // $ExpectType JQuery + $('p').innerWidth('200px'); + + // $ExpectType JQuery + $('p').innerWidth(400); + + // $ExpectType JQuery + $('p').innerWidth(function(index, width) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType number + width; + + return '200px'; + }); + + // $ExpectType JQuery + $('p').innerWidth(function(index, width) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType number + width; + + return 400; + }); + + // $ExpectType number | undefined + $('p').innerWidth(); + } + + function outerWidth() { + // $ExpectType JQuery + $('p').outerWidth('200px'); + + // $ExpectType JQuery + $('p').outerWidth(400); + + // $ExpectType JQuery + $('p').outerWidth(function(index, width) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType number + width; + + return '200px'; + }); + + // $ExpectType JQuery + $('p').outerWidth(function(index, width) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType number + width; + + return 400; + }); + + // $ExpectType number | undefined + $('p').outerWidth(); + + // $ExpectType number | undefined + $('p').outerWidth(true); + } + + function offset() { + // $ExpectType JQuery + $('p').offset({ + left: 20, + top: 50 + }); + + // $ExpectType JQuery + $('p').offset(function(index, coords) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType Coordinates + coords; + + return { + left: 20, + top: 50 + }; + }); + + // $ExpectType Coordinates + $('p').offset(); + } + + function position() { + // $ExpectType Coordinates + $('p').position(); + } + + function scrollLeft() { + // $ExpectType JQuery + $('p').scrollLeft(200); + + // $ExpectType number + $('p').scrollLeft(); + } + + function scrollTop() { + // $ExpectType JQuery + $('p').scrollTop(200); + + // $ExpectType number + $('p').scrollTop(); + } + + function addClass() { + // $ExpectType JQuery + $('p').addClass('className'); + + // $ExpectType JQuery + $('p').addClass(function(index, currentClassName) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + currentClassName; + + return 'className'; + }); + } + + function hasClass() { + // $ExpectType boolean + $('p').hasClass('className'); + } + + function removeClass() { + // $ExpectType JQuery + $('p').removeClass('className'); + + // $ExpectType JQuery + $('p').removeClass(function(index, currentClassName) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + currentClassName; + + return 'className'; + }); + + // $ExpectType JQuery + $('p').removeClass(); + } + + function toggleClass() { + // $ExpectType JQuery + $('p').toggleClass('className', true); + + // $ExpectType JQuery + $('p').toggleClass(function(index, className, state) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + className; + // $ExpectType true + state; + + return 'className'; + }, true); + + // $ExpectType JQuery + $('p').toggleClass('className'); + + // $ExpectType JQuery + $('p').toggleClass(function(index, className, state) { + // $ExpectType HTMLElement + this; + // $ExpectType number + index; + // $ExpectType string + className; + // $ExpectType boolean + state; + + return 'className'; + }); + + // $ExpectType JQuery + $('p').toggleClass(false); + + // $ExpectType JQuery + $('p').toggleClass(); + } + } + + function data() { + function data() { + // $ExpectType any + $('p').data('myData', undefined); + + // $ExpectType JQuery + $('p').data('myData', {}); + + // $ExpectType JQuery + $('p').data({ + myData1: {}, + myData2: false + }); + + // $ExpectType any + $('p').data('myData'); + + // $ExpectType PlainObject + $('p').data(); + } + + function removeData() { + // $ExpectType JQuery + $('p').removeData('myData'); + + // $ExpectType JQuery + $('p').removeData([ + 'myData1', + 'myData2' + ]); + + // $ExpectType JQuery + $('p').removeData(); + } + } + + function effects() { + function animate() { + // $ExpectType JQuery + $('#book').animate({ + width: ['toggle', 'swing'], + height: 200, + opacity: 'toggle' + }, 5000, 'linear', function() { + // $ExpectType HTMLElement + this; + + $(this).after('
Animation complete.
'); + }); + + // $ExpectType JQuery + $('#book').animate({ + width: ['toggle', 'swing'], + height: 200, + opacity: 'toggle' + }, 5000, 'linear'); + + // $ExpectType JQuery + $('#book').animate({ + width: ['toggle', 'swing'], + height: 200, + opacity: 'toggle' + }, 5000, function() { + // $ExpectType HTMLElement + this; + + $(this).after('
Animation complete.
'); + }); + + // $ExpectType JQuery + $('#book').animate({ + width: ['toggle', 'swing'], + height: 200, + opacity: 'toggle' + }, 'linear', function() { + // $ExpectType HTMLElement + this; + + $(this).after('
Animation complete.
'); + }); + + // $ExpectType JQuery + $('#book').animate({ + width: ['toggle', 'swing'], + height: 200, + opacity: 'toggle' + }, 5000); + + // $ExpectType JQuery + $('#book').animate({ + width: ['toggle', 'swing'], + height: 200, + opacity: 'toggle' + }, 'linear'); + + // $ExpectType JQuery + $('#book').animate({ + width: ['toggle', 'swing'], + height: 200, + opacity: 'toggle' + }, function() { + // $ExpectType HTMLElement + this; + + $(this).after('
Animation complete.
'); + }); + + // $ExpectType JQuery + $('#book').animate({ + width: 'toggle', + height: 'toggle' + }, { + duration: 5000, + specialEasing: { + width: 'linear', + height: 'easeOutBounce' + }, + complete() { + $(this).after('
Animation complete.
'); + } + }); + + // $ExpectType JQuery + $('#book').animate({ + width: ['toggle', 'swing'], + height: 200, + opacity: 'toggle' + }); + } + + function fadeIn() { + // $ExpectType JQuery + $('p').fadeIn(5000, 'linear', function() { + // $ExpectType HTMLElement + this; + }); + + // $ExpectType JQuery + $('p').fadeIn(5000, 'linear'); + + // $ExpectType JQuery + $('p').fadeIn(5000, function() { + // $ExpectType HTMLElement + this; + }); + + // $ExpectType JQuery + $('p').fadeIn('linear', function() { + // $ExpectType HTMLElement + this; + }); + + // $ExpectType JQuery + $('p').fadeIn(5000); + + // $ExpectType JQuery + $('p').fadeIn('linear'); + + // $ExpectType JQuery + $('p').fadeIn(function() { + // $ExpectType HTMLElement + this; + }); + + // $ExpectType JQuery + $('p').fadeIn({ + duration: 5000, + specialEasing: { + width: 'linear', + height: 'easeOutBounce' + }, + complete() { + $(this).after('
Animation complete.
'); + } + }); + + // $ExpectType JQuery + $('p').fadeIn(); + } + + function fadeOut() { + // $ExpectType JQuery + $('p').fadeOut(5000, 'linear', function() { + // $ExpectType HTMLElement + this; + }); + + // $ExpectType JQuery + $('p').fadeOut(5000, 'linear'); + + // $ExpectType JQuery + $('p').fadeOut(5000, function() { + // $ExpectType HTMLElement + this; + }); + + // $ExpectType JQuery + $('p').fadeOut('linear', function() { + // $ExpectType HTMLElement + this; + }); + + // $ExpectType JQuery + $('p').fadeOut(5000); + + // $ExpectType JQuery + $('p').fadeOut('linear'); + + // $ExpectType JQuery + $('p').fadeOut(function() { + // $ExpectType HTMLElement + this; + }); + + // $ExpectType JQuery + $('p').fadeOut({ + duration: 5000, + specialEasing: { + width: 'linear', + height: 'easeOutBounce' + }, + complete() { + $(this).after('
Animation complete.
'); + } + }); + + // $ExpectType JQuery + $('p').fadeOut(); + } + + function fadeToggle() { + // $ExpectType JQuery + $('p').fadeToggle(5000, 'linear', function() { + // $ExpectType HTMLElement + this; + }); + + // $ExpectType JQuery + $('p').fadeToggle(5000, 'linear'); + + // $ExpectType JQuery + $('p').fadeToggle(5000, function() { + // $ExpectType HTMLElement + this; + }); + + // $ExpectType JQuery + $('p').fadeToggle('linear', function() { + // $ExpectType HTMLElement + this; + }); + + // $ExpectType JQuery + $('p').fadeToggle(5000); + + // $ExpectType JQuery + $('p').fadeToggle('linear'); + + // $ExpectType JQuery + $('p').fadeToggle(function() { + // $ExpectType HTMLElement + this; + }); + + // $ExpectType JQuery + $('p').fadeToggle({ + duration: 5000, + specialEasing: { + width: 'linear', + height: 'easeOutBounce' + }, + complete() { + $(this).after('
Animation complete.
'); + } + }); + + // $ExpectType JQuery + $('p').fadeToggle(); + } + + function slideDown() { + // $ExpectType JQuery + $('p').slideDown(5000, 'linear', function() { + // $ExpectType HTMLElement + this; + }); + + // $ExpectType JQuery + $('p').slideDown(5000, 'linear'); + + // $ExpectType JQuery + $('p').slideDown(5000, function() { + // $ExpectType HTMLElement + this; + }); + + // $ExpectType JQuery + $('p').slideDown('linear', function() { + // $ExpectType HTMLElement + this; + }); + + // $ExpectType JQuery + $('p').slideDown(5000); + + // $ExpectType JQuery + $('p').slideDown('linear'); + + // $ExpectType JQuery + $('p').slideDown(function() { + // $ExpectType HTMLElement + this; + }); + + // $ExpectType JQuery + $('p').slideDown({ + duration: 5000, + specialEasing: { + width: 'linear', + height: 'easeOutBounce' + }, + complete() { + $(this).after('
Animation complete.
'); + } + }); + + // $ExpectType JQuery + $('p').slideDown(); + } + + function slideToggle() { + // $ExpectType JQuery + $('p').slideToggle(5000, 'linear', function() { + // $ExpectType HTMLElement + this; + }); + + // $ExpectType JQuery + $('p').slideToggle(5000, 'linear'); + + // $ExpectType JQuery + $('p').slideToggle(5000, function() { + // $ExpectType HTMLElement + this; + }); + + // $ExpectType JQuery + $('p').slideToggle('linear', function() { + // $ExpectType HTMLElement + this; + }); + + // $ExpectType JQuery + $('p').slideToggle(5000); + + // $ExpectType JQuery + $('p').slideToggle('linear'); + + // $ExpectType JQuery + $('p').slideToggle(function() { + // $ExpectType HTMLElement + this; + }); + + // $ExpectType JQuery + $('p').slideToggle({ + duration: 5000, + specialEasing: { + width: 'linear', + height: 'easeOutBounce' + }, + complete() { + $(this).after('
Animation complete.
'); + } + }); + + // $ExpectType JQuery + $('p').slideToggle(); + } + + function slideUp() { + // $ExpectType JQuery + $('p').slideUp(5000, 'linear', function() { + // $ExpectType HTMLElement + this; + }); + + // $ExpectType JQuery + $('p').slideUp(5000, 'linear'); + + // $ExpectType JQuery + $('p').slideUp(5000, function() { + // $ExpectType HTMLElement + this; + }); + + // $ExpectType JQuery + $('p').slideUp('linear', function() { + // $ExpectType HTMLElement + this; + }); + + // $ExpectType JQuery + $('p').slideUp(5000); + + // $ExpectType JQuery + $('p').slideUp('linear'); + + // $ExpectType JQuery + $('p').slideUp(function() { + // $ExpectType HTMLElement + this; + }); + + // $ExpectType JQuery + $('p').slideUp({ + duration: 5000, + specialEasing: { + width: 'linear', + height: 'easeOutBounce' + }, + complete() { + $(this).after('
Animation complete.
'); + } + }); + + // $ExpectType JQuery + $('p').slideUp(); + } + + function fadeTo() { + // $ExpectType JQuery + $('p').fadeTo(5000, 0.9, 'linear', function() { + // $ExpectType HTMLElement + this; + }); + + // $ExpectType JQuery + $('p').fadeTo(5000, 0.9, 'linear'); + + // $ExpectType JQuery + $('p').fadeTo(5000, 0.9, function() { + // $ExpectType HTMLElement + this; + }); + + // $ExpectType JQuery + $('p').fadeTo(5000, 0.9); + } + + function toggle() { + // $ExpectType JQuery + $('p').toggle(5000, 'linear', function() { + // $ExpectType HTMLElement + this; + }); + + // $ExpectType JQuery + $('p').toggle(5000, 'linear'); + + // $ExpectType JQuery + $('p').toggle(5000, function() { + // $ExpectType HTMLElement + this; + }); + + // $ExpectType JQuery + $('p').toggle(5000); + + // $ExpectType JQuery + $('p').toggle(function() { + // $ExpectType HTMLElement + this; + }); + + // $ExpectType JQuery + $('p').toggle('linear'); + + // $ExpectType JQuery + $('p').toggle(true); + + // $ExpectType JQuery + $('p').toggle(); + } + + function hide() { + // $ExpectType JQuery + $('p').hide(5000, 'linear', function() { + // $ExpectType HTMLElement + this; + }); + + // $ExpectType JQuery + $('p').hide(5000, 'linear'); + + // $ExpectType JQuery + $('p').hide(5000, function() { + // $ExpectType HTMLElement + this; + }); + + // $ExpectType JQuery + $('p').hide(5000); + + // $ExpectType JQuery + $('p').hide(function() { + // $ExpectType HTMLElement + this; + }); + + // $ExpectType JQuery + $('p').hide({ + duration: 5000, + specialEasing: { + width: 'linear', + height: 'easeOutBounce' + }, + complete() { + $(this).after('
Animation complete.
'); + } + }); + + // $ExpectType JQuery + $('p').hide(); + } + + function show() { + // $ExpectType JQuery + $('p').show(5000, 'linear', function() { + // $ExpectType HTMLElement + this; + }); + + // $ExpectType JQuery + $('p').show(5000, 'linear'); + + // $ExpectType JQuery + $('p').show(5000, function() { + // $ExpectType HTMLElement + this; + }); + + // $ExpectType JQuery + $('p').show(5000); + + // $ExpectType JQuery + $('p').show(function() { + // $ExpectType HTMLElement + this; + }); + + // $ExpectType JQuery + $('p').show({ + duration: 5000, + specialEasing: { + width: 'linear', + height: 'easeOutBounce' + }, + complete() { + $(this).after('
Animation complete.
'); + } + }); + + // $ExpectType JQuery + $('p').show(); + } + + function clearQueue() { + // $ExpectType JQuery + $('p').clearQueue('myQueue'); + + // $ExpectType JQuery + $('p').clearQueue(); + } + + function delay() { + // $ExpectType JQuery + $('p').delay('fast', 'myQueue'); + + // $ExpectType JQuery + $('p').delay('slow'); + } + + function dequeue() { + // $ExpectType JQuery + $('p').dequeue('myQueue'); + + // $ExpectType JQuery + $('p').dequeue(); + } + + function finish() { + // $ExpectType JQuery + $('p').finish('myQueue'); + + // $ExpectType JQuery + $('p').finish(); + } + + function queue() { + // $ExpectType JQuery + $('p').queue('myQueue', function(next) { + // $ExpectType HTMLElement + this; + // $ExpectType () => void + next; + }); + + // $ExpectType JQuery + $('p').queue('myQueue', [ + function(next) { + // $ExpectType HTMLElement + this; + // $ExpectType () => void + next; + }, + function(next) { + // $ExpectType HTMLElement + this; + // $ExpectType () => void + next; + } + ]); + + // $ExpectType JQuery + $('p').queue(function(next) { + // $ExpectType HTMLElement + this; + // $ExpectType () => void + next; + }); + + // $ExpectType JQuery + $('p').queue([ + function(next) { + // $ExpectType HTMLElement + this; + // $ExpectType () => void + next; + }, + function(next) { + // $ExpectType HTMLElement + this; + // $ExpectType () => void + next; + } + ]); + + // $ExpectType Queue + $('p').queue('myQueue'); + + // $ExpectType Queue + $('p').queue(); + } + + function stop() { + // $ExpectType JQuery + $('p').stop('myQueue', true, false); + + // $ExpectType JQuery + $('p').stop('myQueue', true); + + // $ExpectType JQuery + $('p').stop(true, false); + + // $ExpectType JQuery + $('p').stop(true); + } + } + function on() { function false_handler_shorthand() { $().on('events', false); @@ -131,6 +1498,70 @@ function JQueryStatic() { } } +function EffectsOptions() { + $('p').show({ + always(animation, jumpToEnd) { + // $ExpectType HTMLElement + this; + // $ExpectType Promise + animation; + // $ExpectType boolean + jumpToEnd; + }, + complete() { + // $ExpectType HTMLElement + this; + }, + done(animation, jumpToEnd) { + // $ExpectType HTMLElement + this; + // $ExpectType Promise + animation; + // $ExpectType boolean + jumpToEnd; + }, + duration: 5000, + easing: 'linear', + fail(animation, jumpToEnd) { + // $ExpectType HTMLElement + this; + // $ExpectType Promise + animation; + // $ExpectType boolean + jumpToEnd; + }, + progress(animation, progress, remainingMs) { + // $ExpectType HTMLElement + this; + // $ExpectType Promise + animation; + // $ExpectType number + progress; + // $ExpectType number + remainingMs; + }, + queue: true, + specialEasing: { + width: 'linear', + height: 'easeOutBounce' + }, + start(animation) { + // $ExpectType HTMLElement + this; + // $ExpectType Promise + animation; + }, + step(now, tween) { + // $ExpectType HTMLElement + this; + // $ExpectType number + now; + // $ExpectType Tween + tween; + } + }); +} + function JQuery_Event() { function type_guard(e: object) { if (e instanceof JQuery.Event) { @@ -184,7 +1615,9 @@ function jqXHR() { $.ajax('/echo').fail((jqXHR, textStatus, errorThrown) => { // $ExpectType jqXHR jqXHR; - // $ExpectType "abort" | "timeout" | "error" | "parsererror" | null + // This test is flaky + // Should be 'ErrorTextStatus | null' and should be able to handle it out of order + // $ExpectType "timeout" | "error" | "abort" | "parsererror" | null textStatus; // $ExpectType string errorThrown; diff --git a/types/jquery/test/example-tests.ts b/types/jquery/test/example-tests.ts index d2c0a8464d..961e239192 100644 --- a/types/jquery/test/example-tests.ts +++ b/types/jquery/test/example-tests.ts @@ -1822,8 +1822,8 @@ function examples() { } function finish_0() { - var horiz = $('#path').width() - 20, - vert = $('#path').height() - 20; + var horiz = $('#path').width()! - 20, + vert = $('#path').height()! - 20; var btns: { [key: string]: () => void; } = { bstt: function() { @@ -1992,13 +1992,13 @@ function examples() { } $('#getp').click(function() { - showHeight('paragraph', $('p').height()); + showHeight('paragraph', $('p').height()!); }); $('#getd').click(function() { - showHeight('document', $(document).height()); + showHeight('document', $(document).height()!); }); $('#getw').click(function() { - showHeight('window', $(window).height()); + showHeight('window', $(window).height()!); }); } @@ -4935,13 +4935,13 @@ function examples() { } $('#getp').click(function() { - showWidth('paragraph', $('p').width()); + showWidth('paragraph', $('p').width()!); }); $('#getd').click(function() { - showWidth('document', $(document).width()); + showWidth('document', $(document).width()!); }); $('#getw').click(function() { - showWidth('window', $(window).width()); + showWidth('window', $(window).width()!); }); } diff --git a/types/jquery/test/jquery-no-window-module-tests.ts b/types/jquery/test/jquery-no-window-module-tests.ts new file mode 100644 index 0000000000..700be9d60c --- /dev/null +++ b/types/jquery/test/jquery-no-window-module-tests.ts @@ -0,0 +1,8 @@ +import jQueryFactory = require('jquery'); + +const jq = jQueryFactory(window); +// ExpectType will report 'jq' as 'JQueryStatic & JQuery' even though the compiler seems to know that 'jq' is 'JQueryStatic' +// // $ExpectType JQueryStatic +// jq; +// jq === jQuery(); +jq === jQuery; diff --git a/types/jquery/test/jquery-slim-no-window-module-tests.ts b/types/jquery/test/jquery-slim-no-window-module-tests.ts new file mode 100644 index 0000000000..fdbd34e06c --- /dev/null +++ b/types/jquery/test/jquery-slim-no-window-module-tests.ts @@ -0,0 +1,8 @@ +import jQueryFactory = require('jquery/dist/jquery.slim'); + +const jq = jQueryFactory(window); +// ExpectType will report 'jq' as 'JQueryStatic & JQuery' even though the compiler seems to know that 'jq' is 'JQueryStatic' +// // $ExpectType JQueryStatic +// jq; +// jq === jQuery(); +jq === jQuery; diff --git a/types/jquery/test/jquery-slim-window-module-tests.ts b/types/jquery/test/jquery-slim-window-module-tests.ts new file mode 100644 index 0000000000..8072811f08 --- /dev/null +++ b/types/jquery/test/jquery-slim-window-module-tests.ts @@ -0,0 +1,8 @@ +import * as jq from 'jquery/dist/jquery.slim'; + +const $window = jq(window); +// ExpectType will report 'jq' as 'JQueryStatic & JQuery' even though the compiler seems to know that 'jq' is 'JQuery' +// // $ExpectType JQuery +// $window; +// $window === jq; +$window === jq(); diff --git a/types/jquery/test/jquery-window-module-tests.ts b/types/jquery/test/jquery-window-module-tests.ts new file mode 100644 index 0000000000..85f2c4c4ea --- /dev/null +++ b/types/jquery/test/jquery-window-module-tests.ts @@ -0,0 +1,8 @@ +import * as jq from 'jquery'; + +const $window = jq(window); +// ExpectType will report 'jq' as 'JQueryStatic & JQuery' even though the compiler seems to know that 'jq' is 'JQuery' +// // $ExpectType JQuery +// $window; +// $window === jq; +$window === jq(); diff --git a/types/jquery/test/module-tests.ts b/types/jquery/test/module-tests.ts deleted file mode 100644 index b55dee0370..0000000000 --- a/types/jquery/test/module-tests.ts +++ /dev/null @@ -1,7 +0,0 @@ -import factory = require('jquery'); -const jq = factory(window); -jq.noop(); - -import slimFactory = require('jquery/dist/jquery.slim'); -const slim = slimFactory(window); -slim.noop(); diff --git a/types/jquery/tsconfig.json b/types/jquery/tsconfig.json index ebfa01c813..21d35a63da 100644 --- a/types/jquery/tsconfig.json +++ b/types/jquery/tsconfig.json @@ -22,6 +22,9 @@ "jquery-tests.ts", "test/example-tests.ts", "test/longdesc-tests.ts", - "test/module-tests.ts" + "test/jquery-no-window-module-tests.ts", + "test/jquery-window-module-tests.ts", + "test/jquery-slim-no-window-module-tests.ts", + "test/jquery-slim-window-module-tests.ts" ] }