diff --git a/types/select2/index.d.ts b/types/select2/index.d.ts index 0d7da1fe0f..3f04a52414 100644 --- a/types/select2/index.d.ts +++ b/types/select2/index.d.ts @@ -13,15 +13,14 @@ interface Select2QueryOptions { callback?: (result: { results: any; more?: boolean; context?: any; }) => void; } -interface AjaxFunction { - (settings: JQueryAjaxSettings, success?: (data: any) => null, failure?: () => null): JQueryXHR; -} +type AjaxFunction = + (settings: JQueryAjaxSettings, success?: (data: any) => null, failure?: () => null) => JQueryXHR; interface Select2AjaxOptions extends JQueryAjaxSettings { transport?: AjaxFunction; /** - * Url to make request to, Can be string or a function returning a string. - */ + * Url to make request to, can be string or a function returning a string. + */ url?: any; dataType?: string; delay?: number; @@ -80,8 +79,8 @@ interface Select2Options { escapeMarkup?: (markup: string) => string; theme?: string; /** - * Template can return both plain string that will be HTML escaped and a jquery object that can render HTML - */ + * Template can return both plain string that will be HTML escaped and a jquery object that can render HTML + */ templateSelection?: (object: Select2SelectionObject, container: JQuery) => any; templateResult?: (object: Select2SelectionObject) => any; language?: string | string[] | {}; diff --git a/types/select2/select2-tests.ts b/types/select2/select2-tests.ts index 29da3b6f81..0a5458063a 100644 --- a/types/select2/select2-tests.ts +++ b/types/select2/select2-tests.ts @@ -12,9 +12,9 @@ $("#e2_3").select2({ $("#e3").select2({ minimumInputLength: 2 }); -function format(state) { +function format(state: any) { if (!state.id) return state.text; - return "" + state.text; + return `` + state.text; } $("#e4").select2({ formatResult: format, @@ -22,11 +22,11 @@ $("#e4").select2({ }); $("#e5").select2({ minimumInputLength: 1, - query: function (query) { - var data = { results: [] }, i, j, s; - for (i = 1; i < 5; i++) { - s = ""; - for (j = 0; j < i; j++) { s = s + query.term; } + query(query) { + const data = { results: [] as IdTextPair[] }; + for (let i = 1; i < 5; i++) { + let s = ""; + for (let j = 0; j < i; j++) { s = s + query.term; } data.results.push({ id: query.term + i, text: s }); } } @@ -37,7 +37,7 @@ $("#e10").select2({ data: [{ id: 0, text: 'enhancement' }, { id: 1, text: 'bug' }, { id: 2, text: 'duplicate' }, { id: 3, text: 'invalid' }, { id: 4, text: 'wontfix' }] }); -var data = [{ id: 0, tag: 'enhancement' }, { id: 1, tag: 'bug' }, { id: 2, tag: 'duplicate' }, { id: 3, tag: 'invalid' }, { id: 4, tag: 'wontfix' }]; +const data = [{ id: 0, tag: 'enhancement' }, { id: 1, tag: 'bug' }, { id: 2, tag: 'duplicate' }, { id: 3, tag: 'invalid' }, { id: 4, tag: 'wontfix' }]; $("#e10_2").select2({ data: { results: data, text: 'tag' }, @@ -46,11 +46,18 @@ $("#e10_2").select2({ }); $("#e10_3").select2({ - data: { results: data, text: function (item) { console.log('called with', item); return item.tag; } }, + data: { + results: data, + text: (item: {tag: string}) => { + console.log('called with', item); + return item.tag; + }}, formatSelection: format, formatResult: format }); -var movieFormatResult, movieFormatSelection; + +let movieFormatResult; +let movieFormatSelection; $("#e6").select2({ placeholder: "Search for a movie", minimumInputLength: 1, @@ -58,14 +65,14 @@ $("#e6").select2({ url: "http://api.rottentomatoes.com/api/public/v1.0/movies.json", dataType: 'jsonp', cache: false, - data: function (params, page) { + data(params, page) { return { q: params.term, page_limit: 10, apikey: "ju6z9mjyajq2djue3gbvv26t" }; }, - results: function (data, page) { + results(data, page) { return { results: data.movies }; } }, @@ -73,20 +80,22 @@ $("#e6").select2({ formatSelection: movieFormatSelection, dropdownCssClass: "bigdrop" }); + +let t: ArrayLike; $("#e6").select2({ placeholder: "Search for a movie", minimumInputLength: 1, ajax: { - url: () => { return "http://api.rottentomatoes.com/api/public/v1.0/movies.json"; }, + url: () => "http://api.rottentomatoes.com/api/public/v1.0/movies.json", dataType: 'jsonp', - data: function (params, page) { + data(params, page) { return { q: params.term, page_limit: 10, apikey: "ju6z9mjyajq2djue3gbvv26t" }; }, - results: function (data, page) { + results(data, page) { return { results: data.movies }; } }, @@ -102,14 +111,14 @@ $("#e6").select2({ type: 'GET', dataType: 'jsonp', cache: false, - data: function (params, page) { + data(params, page) { return { q: params.term, page_limit: 10, apikey: "ju6z9mjyajq2djue3gbvv26t" }; }, - results: function (data, page) { + results(data, page) { return { results: data.movies }; } }, @@ -124,17 +133,17 @@ $("#e7").select2({ url: "http://api.rottentomatoes.com/api/public/v1.0/movies.json", dataType: 'jsonp', delay: 100, - data: function (params, page) { + data(params, aPage) { return { q: params.term, page_limit: 10, - page: page, + page: aPage, apikey: "ju6z9mjyajq2djue3gbvv26t" }; }, - results: function (data, page) { - var more = (page * 10) < data.total; - return { results: data.movies, more: more }; + results(data, page) { + const moreValue = (page * 10) < data.total; + return { results: data.movies, more: moreValue }; } }, formatResult: movieFormatResult, @@ -142,7 +151,7 @@ $("#e7").select2({ dropdownCssClass: "bigdrop" }); -function sort(elements) { +function sort(elements: any) { return elements.sort(); } $("#e20").select2({ @@ -150,66 +159,65 @@ $("#e20").select2({ }); $("#e8").select2(); -$("#e8_get").click(function () { alert("Selected value is: " + $("#e8").select2("val")); }); -$("#e8_set").click(function () { $("#e8").select2("val", "CA"); }); -$("#e8_cl").click(function () { $("#e8").select2("val", ""); }); -$("#e8_get2").click(function () { alert("Selected data is: " + JSON.stringify($("#e8").select2("data"))); }); -$("#e8_set2").click(function () { $("#e8").select2("data", { id: "CA", text: "California" }); }); -$("#e8_open").click(function () { $("#e8").select2("open"); }); -$("#e8_close").click(function () { $("#e8").select2("close"); }); +$("#e8_get").click(() => alert("Selected value is: " + $("#e8").select2("val"))); +$("#e8_set").click(() => $("#e8").select2("val", "CA")); +$("#e8_cl").click(() => $("#e8").select2("val", "")); +$("#e8_get2").click(() => alert("Selected data is: " + JSON.stringify($("#e8").select2("data")))); +$("#e8_set2").click(() => $("#e8").select2("data", { id: "CA", text: "California" })); +$("#e8_open").click(() => $("#e8").select2("open")); +$("#e8_close").click(() => $("#e8").select2("close")); $("#e8_2").select2(); -$("#e8_2_get").click(function () { alert("Selected value is: " + $("#e8_2").select2("val")); }); -$("#e8_2_set").click(function () { $("#e8_2").select2("val", ["CA", "MA"]); }); -$("#e8_2_get2").click(function () { alert("Selected value is: " + JSON.stringify($("#e8_2").select2("data"))); }); -$("#e8_2_set2").click(function () { $("#e8_2").select2("data", [{ id: "CA", text: "California" }, { id: "MA", text: "Massachusetts" }]); }); -$("#e8_2_cl").click(function () { $("#e8_2").select2("val", ""); }); -$("#e8_2_open").click(function () { $("#e8_2").select2("open"); }); -$("#e8_2_close").click(function () { $("#e8_2").select2("close"); }); +$("#e8_2_get").click(() => alert("Selected value is: " + $("#e8_2").select2("val"))); +$("#e8_2_set").click(() => $("#e8_2").select2("val", ["CA", "MA"])); +$("#e8_2_get2").click(() => alert("Selected value is: " + JSON.stringify($("#e8_2").select2("data")))); +$("#e8_2_set2").click(() => $("#e8_2").select2("data", [{ id: "CA", text: "California" }, { id: "MA", text: "Massachusetts" }])); +$("#e8_2_cl").click(() => $("#e8_2").select2("val", "")); +$("#e8_2_open").click(() => $("#e8_2").select2("open")); +$("#e8_2_close").click(() => $("#e8_2").select2("close")); $("#e11").select2({ placeholder: "Select report type", allowClear: true, data: [{ id: 0, text: 'story' }, { id: 1, text: 'bug' }, { id: 2, text: 'task' }] }); $("#e11_2").select2({ - createSearchChoice: function (term, data) { if ($(data).filter(function () { return this.textContent.localeCompare(term) === 0; }).length === 0) { return { id: term, text: term }; } }, multiple: true, data: [{ id: 0, text: 'story' }, { id: 1, text: 'bug' }, { id: 2, text: 'task' }] }); -function log(e) { - var item = $("
  • " + e + "
  • "); +function log(e: string) { + const item = $(`
  • ${e}
  • `); $("#events_11").append(item); - item.animate({ opacity: 1 }, 10000, 'linear', function () { item.animate({ opacity: 0 }, 2000, 'linear', function () { item.remove(); }); }); + item.animate({ opacity: 1 }, 10000, 'linear', () => item.animate({ opacity: 0 }, 2000, 'linear', () => item.remove())); } $("#e11") // TS 0.9.5: correct overload not resolved https://typescript.codeplex.com/discussions/472172 - .on("change", function (e: Select2JQueryEventObject) { log(JSON.stringify({ val: e.val, added: e.added, removed: e.removed })); }) - .on("open", function () { log("open"); }); + .on("change", (e: Select2JQueryEventObject) => log(JSON.stringify({ val: e.val, added: e.added, removed: e.removed }))) + .on("open", () => log("open")); $("#e11_2") - .on("change", function (e: Select2JQueryEventObject) { log(JSON.stringify({ val: e.val, added: e.added, removed: e.removed })); }) - .on("open", function () { log("open"); }); + .on("change", (e: Select2JQueryEventObject) => log(JSON.stringify({ val: e.val, added: e.added, removed: e.removed }))) + .on("open", () => log("open")); $("#e12").select2({ tags: ["red", "green", "blue"] }); $("#e20").select2({ tags: ["red", "green", "blue"], tokenSeparators: [",", " "] }); $("#e13").select2(); -$("#e13_ca").click(function () { $("#e13").val("CA").trigger("change"); }); -$("#e13_ak_co").click(function () { $("#e13").val(["AK", "CO"]).trigger("change"); }); +$("#e13_ca").click(() => $("#e13").val("CA").trigger("change")); +$("#e13_ak_co").click(() => $("#e13").val(["AK", "CO"]).trigger("change")); $("#e14").val(["AL", "AZ"]).select2(); -$("#e14_init").click(function () { $("#e14").select2(); }); -$("#e14_destroy").click(function () { $("#e14").select2("destroy"); }); +$("#e14_init").click(() => $("#e14").select2()); +$("#e14_destroy").click(() => $("#e14").select2("destroy")); $("#e15").select2({ tags: ["red", "green", "blue", "orange", "white", "black", "purple", "cyan", "teal"] }); -$("#e15").on("change", function () { $("#e15_val").html($("#e15").val() as string); }); +$("#e15").on("change", () => $("#e15_val").html($("#e15").val() as string)); $("#e16").select2(); $("#e16_2").select2(); -$("#e16_enable").click(function () { $("#e16,#e16_2").select2("enable"); }); -$("#e16_disable").click(function () { $("#e16,#e16_2").select2("disable"); }); +$("#e16_enable").click(() => $("#e16,#e16_2").select2("enable")); +$("#e16_disable").click(() => $("#e16,#e16_2").select2("disable")); $("#e17").select2({ - matcher: function (term, text) { return text.toUpperCase().indexOf(term.toUpperCase()) == 0; } + matcher: (term, text) => text.toUpperCase().indexOf(term.toUpperCase()) === 0 }); $("#e17_2").select2({ - matcher: function (term, text, opt) { + matcher: (term, text, opt) => { return text.toUpperCase().indexOf(term.toUpperCase()) >= 0 || opt.attr("alt").toUpperCase().indexOf(term.toUpperCase()) >= 0; } diff --git a/types/select2/tsconfig.json b/types/select2/tsconfig.json index 292cf878b4..257d07ea49 100644 --- a/types/select2/tsconfig.json +++ b/types/select2/tsconfig.json @@ -5,8 +5,8 @@ "es6", "dom" ], - "noImplicitAny": false, - "noImplicitThis": false, + "noImplicitAny": true, + "noImplicitThis": true, "strictNullChecks": false, "strictFunctionTypes": true, "baseUrl": "../", diff --git a/types/select2/tslint.json b/types/select2/tslint.json index a41bf5d19a..08b1465cd6 100644 --- a/types/select2/tslint.json +++ b/types/select2/tslint.json @@ -1,79 +1,6 @@ { "extends": "dtslint/dt.json", "rules": { - "adjacent-overload-signatures": false, - "array-type": false, - "arrow-return-shorthand": false, - "ban-types": false, - "callable-types": false, - "comment-format": false, - "dt-header": false, - "eofline": false, - "export-just-namespace": false, - "import-spacing": false, - "interface-name": false, - "interface-over-type-literal": false, - "jsdoc-format": false, - "max-line-length": false, - "member-access": false, - "new-parens": false, - "no-any-union": false, - "no-boolean-literal-compare": false, - "no-conditional-assignment": false, - "no-consecutive-blank-lines": false, - "no-construct": false, - "no-declare-current-package": false, - "no-duplicate-imports": false, - "no-duplicate-variable": false, - "no-empty-interface": false, - "no-for-in-array": false, - "no-inferrable-types": false, - "no-internal-module": false, - "no-irregular-whitespace": false, - "no-mergeable-namespace": false, - "no-misused-new": false, - "no-namespace": false, - "no-object-literal-type-assertion": false, - "no-padding": false, - "no-redundant-jsdoc": false, - "no-redundant-jsdoc-2": false, - "no-redundant-undefined": false, - "no-reference-import": false, - "no-relative-import-in-test": false, - "no-self-import": false, - "no-single-declare-module": false, - "no-string-throw": false, - "no-unnecessary-callback-wrapper": false, - "no-unnecessary-class": false, - "no-unnecessary-generics": false, - "no-unnecessary-qualifier": false, - "no-unnecessary-type-assertion": false, - "no-useless-files": false, - "no-var-keyword": false, - "no-var-requires": false, - "no-void-expression": false, - "no-trailing-whitespace": false, - "object-literal-key-quotes": false, - "object-literal-shorthand": false, - "one-line": false, - "one-variable-per-declaration": false, - "only-arrow-functions": false, - "prefer-conditional-expression": false, - "prefer-const": false, - "prefer-declare-function": false, - "prefer-for-of": false, - "prefer-method-signature": false, - "prefer-template": false, - "radix": false, - "semicolon": false, - "space-before-function-paren": false, - "space-within-parens": false, - "strict-export-declare-modifiers": false, - "trim-file": false, - "triple-equals": false, - "typedef-whitespace": false, - "unified-signatures": false, - "void-return": false, - "whitespace": false + "unified-signatures": false } }