4

I am having problems defining my $http call in Typescript. Previously I was using .success and .error like this:

this.$http({
            method: "GET",
            url: self.ac.dataServer + url
        })
            .success((data: any, status: number, headers: (headerName: string) => string, config: ng.IRequestConfig): void => {
                self.topics = angular.copy(data);
                self.topicsBase = angular.copy(data);
                this.$state.transitionTo('home.subjects.subject.admin.topics', {
                    subjectId: this.sus.subject.id
                });
            })
            .error((data, status, headers, config): void => {
                self.topics = null;
                self.topicsBase = null;
            })
            .finally((): void => {
            });

Now I changed to use .then:

 this.$http({
            method: "GET",
            url: self.ac.dataServer + url
        })
            .then(
            (response): any => {
                self.topics = angular.copy(response.data);
                self.topicsBase = angular.copy(response.data);
                this.$state.transitionTo('home.subjects.subject.admin.topics', {
                    subjectId: this.sus.subject.id
                });},
            (response): any => {
                self.topics = null;
                self.topicsBase = null;
            }
            );

But this gives me the following errors:

Severity Code Description Project File Line Error TS2322 Type '{}' is not assignable to type 'ITopic[]'. Property 'length' is missing in type '{}'. admin C:\H\admin\admin\app\services\topicservice.ts 214 Severity Code Description Project File Line Error TS2322 Type '{}' is not assignable to type 'ITopic[]'. admin C:\H\admin\admin\app\services\topicservice.ts 215

The errors come up on the lines where I am assigning an angular.copy(data) to self.topics and self.topicsBase

1 Answer 1

2

Argument of type '(data: IHttpPromiseCallbackArg<{}>, status: any, headers: any, config: any) => any' is not assignable to parameter of type '(response: IHttpPromiseCallbackArg<{}>) => any'

You are passing a function that takes more arguments than what is returned by $http().

Fixed :

$http({
    method: "GET",
    url: myURL
})
    .then((response): any => { },(): any => {});
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your help. I am sorry but I realized this after posting the question and just tried a quick edit while you were entering your answer. Now I fixed that but I have another problem. The data coming back used to have a type of :any so it fitted well into self.topics and self.topicsBase. Can you give me a suggestion how I could fix this now.
Can you give me a suggestion how I could fix this now. Specify the generic ... but might as well put a put annotation on response
If you don't mind can you give me an example of what you mean? Are you suggesting one way would be to add <ITopic[]> before angular.copy? Also how could i do this with an annotation on response, that's the one i am really confused about. Thank you very much
Also how could i do this with an annotation on response, that's the one i am really confused about : e.g. : .then((response:ng.IHttpPromiseCallbackArg<{foo:string}>): any => { },(): any => {});
I had some difficulties with the callback arguments but from what I see you answered this question correctly. I will open up another question that's a bit more specific. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.