mirror of
https://github.com/FlipsideCrypto/DefinitelyTyped.git
synced 2026-02-06 10:56:53 +00:00
jQuery improve typing (#15102)
* jQuery improve typing Each callback returns nothing or boolean Each return its first argument isArray, isFunction and isWindow as type guards type has a more strict return type make unique generic functions * Add libs in angular-oauth2 tsconfig * Linting angular-oauth2
This commit is contained in:
parent
468026989e
commit
f4fdcaca9c
@ -1,7 +1,7 @@
|
||||
import * as angular from 'angular';
|
||||
|
||||
angular.module('angular-oauth2-test', ['angular-oauth2'])
|
||||
.config(['OAuthProvider', function(OAuthProvider:angular.oauth2.OAuthProvider){
|
||||
.config(['OAuthProvider', (OAuthProvider: angular.oauth2.OAuthProvider) => {
|
||||
OAuthProvider.configure({
|
||||
baseUrl: 'https://api.website.com',
|
||||
clientId: 'CLIENT_ID',
|
||||
|
||||
@ -2,6 +2,10 @@
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"target": "es6",
|
||||
"lib": [
|
||||
"es6",
|
||||
"dom"
|
||||
],
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": true,
|
||||
|
||||
35
jquery/index.d.ts
vendored
35
jquery/index.d.ts
vendored
@ -1166,25 +1166,28 @@ interface JQueryStatic {
|
||||
* 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 iterated by numeric index, from 0 to length-1. Other objects are iterated via their named properties.
|
||||
*
|
||||
* @param collection The object or array to iterate over.
|
||||
* @param callback The function that will be executed on every object.
|
||||
* @param callback The function that will be executed on every object. Will break the loop by returning false.
|
||||
* @returns the first argument, the object that is iterated.
|
||||
* @see {@link https://api.jquery.com/jQuery.each/#jQuery-each-array-callback}
|
||||
*/
|
||||
each<T>(
|
||||
collection: T[],
|
||||
callback: (indexInArray: number, valueOfElement: T) => any
|
||||
): any;
|
||||
callback: (indexInArray: number, valueOfElement: T) => boolean | void
|
||||
): T[];
|
||||
|
||||
/**
|
||||
* 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 iterated by numeric index, from 0 to length-1. Other objects are iterated via their named properties.
|
||||
*
|
||||
* @param collection The object or array to iterate over.
|
||||
* @param callback The function that will be executed on every object.
|
||||
* @param callback The function that will be executed on every object. Will break the loop by returning false.
|
||||
* @returns the first argument, the object that is iterated.
|
||||
* @see {@link https://api.jquery.com/jQuery.each/#jQuery-each-object-callback}
|
||||
*/
|
||||
each(
|
||||
collection: any,
|
||||
callback: (indexInArray: any, valueOfElement: any) => any
|
||||
): any;
|
||||
each<T extends Object>(
|
||||
collection: T,
|
||||
// TODO: `(keyInObject: keyof T, valueOfElement: T[keyof T])`, when TypeScript 2.1 allowed in repository
|
||||
callback: (keyInObject: string, valueOfElement: any) => boolean | void
|
||||
): T;
|
||||
|
||||
/**
|
||||
* Merge the contents of two or more objects together into the first object.
|
||||
@ -1240,7 +1243,7 @@ interface JQueryStatic {
|
||||
* @param obj Object to test whether or not it is an array.
|
||||
* @see {@link https://api.jquery.com/jQuery.isArray/}
|
||||
*/
|
||||
isArray(obj: any): boolean;
|
||||
isArray(obj: any): obj is Array<any>;
|
||||
/**
|
||||
* Check to see if an object is empty (contains no enumerable properties).
|
||||
*
|
||||
@ -1254,7 +1257,7 @@ interface JQueryStatic {
|
||||
* @param obj Object to test whether or not it is a function.
|
||||
* @see {@link https://api.jquery.com/jQuery.isFunction/}
|
||||
*/
|
||||
isFunction(obj: any): boolean;
|
||||
isFunction(obj: any): obj is Function;
|
||||
/**
|
||||
* Determines whether its argument is a number.
|
||||
*
|
||||
@ -1275,7 +1278,7 @@ interface JQueryStatic {
|
||||
* @param obj Object to test whether or not it is a window.
|
||||
* @see {@link https://api.jquery.com/jQuery.isWindow/}
|
||||
*/
|
||||
isWindow(obj: any): boolean;
|
||||
isWindow(obj: any): obj is Window;
|
||||
/**
|
||||
* Check to see if a DOM node is within an XML document (or is an XML document).
|
||||
*
|
||||
@ -1360,7 +1363,7 @@ interface JQueryStatic {
|
||||
* @param obj Object to get the internal JavaScript [[Class]] of.
|
||||
* @see {@link https://api.jquery.com/jQuery.type/}
|
||||
*/
|
||||
type(obj: any): string;
|
||||
type(obj: any): "array" | "boolean" | "date" | "error" | "function" | "null" | "number" | "object" | "regexp" | "string" | "symbol" | "undefined";
|
||||
|
||||
/**
|
||||
* Sorts an array of DOM elements, in place, with the duplicates removed. Note that this only works on arrays of DOM elements, not strings or numbers.
|
||||
@ -1368,7 +1371,7 @@ interface JQueryStatic {
|
||||
* @param array The Array of DOM elements.
|
||||
* @see {@link https://api.jquery.com/jQuery.unique/}
|
||||
*/
|
||||
unique(array: Element[]): Element[];
|
||||
unique<T extends Element>(array: T[]): T[];
|
||||
|
||||
/**
|
||||
* Parses a string into an array of DOM nodes.
|
||||
@ -3301,10 +3304,10 @@ interface JQuery {
|
||||
/**
|
||||
* Iterate over a jQuery object, executing a function for each matched element.
|
||||
*
|
||||
* @param func A function to execute for each matched element.
|
||||
* @param func A function to execute for each matched element. Can stop the loop by returning false.
|
||||
* @see {@link https://api.jquery.com/each/}
|
||||
*/
|
||||
each(func: (index: number, elem: Element) => any): JQuery;
|
||||
each(func: (index: number, elem: Element) => boolean | void): JQuery;
|
||||
|
||||
/**
|
||||
* Retrieve one of the elements matched by the jQuery object.
|
||||
@ -3457,7 +3460,7 @@ interface JQuery {
|
||||
* @param func A function used as a test for each element in the set. this is the current DOM element.
|
||||
* @see {@link https://api.jquery.com/filter/#filter-function}
|
||||
*/
|
||||
filter(func: (index: number, element: Element) => any): JQuery;
|
||||
filter(func: (index: number, element: Element) => boolean): JQuery;
|
||||
/**
|
||||
* Reduce the set of matched elements to those that match the selector or pass the function's test.
|
||||
*
|
||||
|
||||
@ -100,16 +100,18 @@ function test_ajax() {
|
||||
if (getAllResponseHeaders()) {
|
||||
return getAllResponseHeaders();
|
||||
}
|
||||
var allHeaders = "";
|
||||
$(["Cache-Control", "Content-Language", "Content-Type",
|
||||
"Expires", "Last-Modified", "Pragma"]).each(function (i, header_name) {
|
||||
|
||||
if (xhr.getResponseHeader(header_name)) {
|
||||
allHeaders += header_name + ": " + xhr.getResponseHeader(header_name) + "\n";
|
||||
}
|
||||
return allHeaders;
|
||||
});
|
||||
var allHeaders = "";
|
||||
var headersFieldNames = ["Cache-Control", "Content-Language", "Content-Type",
|
||||
"Expires", "Last-Modified", "Pragma"];
|
||||
$(headersFieldNames).each(function (i, header_name) {
|
||||
if (xhr.getResponseHeader(header_name)) {
|
||||
allHeaders += header_name + ": " + xhr.getResponseHeader(header_name) + "\n";
|
||||
}
|
||||
});
|
||||
return allHeaders;
|
||||
};
|
||||
|
||||
return xhr;
|
||||
};
|
||||
$.ajax({
|
||||
@ -1392,9 +1394,24 @@ function test_detach() {
|
||||
}
|
||||
|
||||
function test_each() {
|
||||
$.each([52, 97], function (index, value) {
|
||||
var numArray: number[];
|
||||
numArray = $.each([1, 2, 3, 4], function (index: number, value: number) {
|
||||
alert(index + ': ' + value);
|
||||
});
|
||||
numArray = $.each([1, 2, 3, 4], function (index: number, value: number) {
|
||||
alert(index + ': ' + value);
|
||||
return value < 2;
|
||||
});
|
||||
|
||||
var res: {one: number, 2: string};
|
||||
res = $.each({ one: 1, 2: "two" }, function(key: string, value: any) {
|
||||
alert(key + ': ' + value);
|
||||
});
|
||||
res = $.each({ one: 1, 2: "two" }, function(key: string, value: any) {
|
||||
alert(key + ': ' + value);
|
||||
return key === "2";
|
||||
});
|
||||
|
||||
var map = {
|
||||
'flammable': 'inflammable',
|
||||
'duh': 'no duh'
|
||||
@ -1404,8 +1421,7 @@ function test_each() {
|
||||
});
|
||||
var arr = ["one", "two", "three", "four", "five"];
|
||||
var obj = { one: 1, two: 2, three: 3, four: 4, five: 5 };
|
||||
// TODO: Should not need explicit type annotation https://github.com/Microsoft/TypeScript/issues/10072
|
||||
jQuery.each<string>(arr, function () {
|
||||
jQuery.each(arr, function () {
|
||||
$("#" + this).text("Mine is " + this + ".");
|
||||
return (this != "three");
|
||||
});
|
||||
@ -1482,9 +1498,10 @@ function test_error() {
|
||||
$(this).hide();
|
||||
})
|
||||
.attr("src", "missing.png");
|
||||
jQuery.error("Oups");
|
||||
jQuery.error = (message?: string) => {
|
||||
console.error(message); return this;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function test_eventParams() {
|
||||
@ -1516,7 +1533,7 @@ function test_eventParams() {
|
||||
function propStopped(e) {
|
||||
var msg = "";
|
||||
if (e.isPropagationStopped()) {
|
||||
msg = "called"
|
||||
msg = "called";
|
||||
} else {
|
||||
msg = "not called";
|
||||
}
|
||||
@ -1703,11 +1720,11 @@ function test_fadeToggle() {
|
||||
function test_filter() {
|
||||
$('li').filter(':even').css('background-color', 'red');
|
||||
$('li').filter(function (index) {
|
||||
return index % 3 == 2;
|
||||
return index % 3 === 2;
|
||||
}).css('background-color', 'red');
|
||||
$("div").css("background", "#b4b0da")
|
||||
.filter(function (index) {
|
||||
return index == 1 || $(this).attr("id") == "fourth";
|
||||
return index === 1 || $(this).attr("id") === "fourth";
|
||||
})
|
||||
.css("border", "3px double red");
|
||||
$("div").filter(document.getElementById("unique"));
|
||||
@ -1879,7 +1896,7 @@ function test_getJSON() {
|
||||
function (data) {
|
||||
$.each(data.items, function (i, item) {
|
||||
$("<img/>").attr("src", item.media.m).appendTo("#images");
|
||||
if (i == 3) return false;
|
||||
if (i === "3") return false;
|
||||
});
|
||||
});
|
||||
$.getJSON("test.js", function (json) {
|
||||
@ -2524,6 +2541,17 @@ function test_is() {
|
||||
});
|
||||
}
|
||||
|
||||
function test_isTypeGuards() {
|
||||
var foo: number[] | ((x: string) => number) | Window;
|
||||
if (jQuery.isArray(foo)) {
|
||||
foo.push(1515);
|
||||
} else if (jQuery.isWindow(foo)) {
|
||||
foo.close();
|
||||
} else if (jQuery.isFunction(foo)) {
|
||||
foo("hello world");
|
||||
}
|
||||
}
|
||||
|
||||
function test_isArray() {
|
||||
$("b").append("" + $.isArray([]));
|
||||
}
|
||||
@ -2582,6 +2610,16 @@ function test_isXMLDoc() {
|
||||
jQuery.isXMLDoc(document.body);
|
||||
}
|
||||
|
||||
function test_unique() {
|
||||
jQuery.unique($('div.foo, div.bar').get());
|
||||
jQuery.unique($('div.foo, div.bar').toArray());
|
||||
|
||||
var divs: HTMLDivElement[];
|
||||
var unique: HTMLDivElement[];
|
||||
unique = jQuery.unique(divs);
|
||||
unique = jQuery.unique<HTMLDivElement>(divs);
|
||||
}
|
||||
|
||||
function test_jQuery() {
|
||||
$('div.foo');
|
||||
$('div.foo').click(function () {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user