0

I am using TypeScript 1.7.5 and the latest jQuery type definition. The following call to $.getJSON() fails with "error TS2346: Supplied parameters do not match any signature of call target"

let url: string = api + '/orgs/' + orgname + '/repos?per_page=100';
$.getJSON(url, function(repos: Repo[]) {
    ...
});

Repo is defined as:

export interface Repo {
    name: string;
    stargazers_count: number;
    forks_count: number;
}

The type definition for getJSON() is:

getJSON(url: string, success?: (data: any, textStatus: string, jqXHR: JQueryXHR) => any): JQueryXHR;

What am I missing?

Update

I found that the error is really coming from chaining a call to error(), which is perfectly legal in regular jQuery. If I remove this call to error() the error goes away. Any idea how I could handle the error from getJSON() in TypeScript?

interface Repo {
    name: string;
    stargazers_count: number;
    forks_count: number;
}
var url = "/echo/json/";

$.getJSON(url, (data: any, textStatus: string, jqXHR: JQueryXHR) => {
    var repos: Repo[] = data;
    //...
    alert(JSON.stringify(repos));
})
.error(function() {
    callback([]);
});
Naresh
  • 23,937
  • 33
  • 132
  • 204

1 Answers1

0

I think this can help:

$.getJSON(url, (data: any, textStatus: string, jqXHR: JQueryXHR) => {
    var repos: Repo[] = data;
    //...
});

Please check the jsfiddle.

Update 1.

You should use "fail" to process errors:

interface Repo {
    name: string;
    stargazers_count: number;
    forks_count: number;
}
var url = "/echo/json/";

$.getJSON(url, (data: any, textStatus: string, jqXHR: JQueryXHR) => {
    var repos: Repo[] = data;
    //...
    alert(JSON.stringify(repos));
})
.fail(function() {
    alert("error!");
    //callback([]);
});
TSV
  • 7,538
  • 1
  • 29
  • 37
  • That's much better than what I had, but it is still giving the same error :-( – Naresh Jan 26 '16 at 13:56
  • I've created jsfiddle (https://jsfiddle.net/q2bnhden/) it compiles and works like a charm for me. – TSV Jan 26 '16 at 14:11
  • Thanks, @TSV. Still no success. I am using `tsc` through an npm script. Must be something wrong with my setup. Still checking... – Naresh Jan 26 '16 at 15:28
  • Found it! The error is really coming from chaining a call to `error()`. Please see my update. Can you think of a solution? – Naresh Jan 27 '16 at 04:03
  • Awesome! Thanks for all your help, @TSV. – Naresh Jan 27 '16 at 11:56