Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What type of Typescript return value should I use for a $http call that returns nothing other than success?

I created a service using Typescript:

class ConfigService implements IConfigService {

    public admin = {};
    public adminBackup = {};
    public user = {};
    public loaded = false;

    constructor(
        private $http: ng.IHttpService,
        private $q: ng.IQService
        ) {

    }

    static $inject = [
        '$http',
        '$q'
    ];

    put = ():ng.IHttpPromise<> => {
        var defer = this.$q.defer();
        if (angular.equals(this.admin, this.adminBackup)) {
            return defer.resolve();
        } else {
            this.$http({
                data: {
                    adminJSON: JSON.stringify(this.admin),
                    userJSON: JSON.stringify(this.user)
                },
                url: '/api/Config/Put',
                method: "PUT"
            })
                .success(function (data) {
                    this.adminBackup = angular.copy(this.admin);
                    this.userBackup = angular.copy(this.user)
                    return defer.resolve();
                });
        }
        return defer.promise;
    };

}

I also created this interface:

interface IConfigService {
     put(): ng.IHttpPromise<>;
}

However the code is giving me an error saying:

Error   3   Cannot convert 'void' to 'ng.IHttpPromise<any>'.    

Error   4   Cannot convert 'ng.IPromise<{}>' to 'ng.IHttpPromise<any>':
    Type 'ng.IPromise<{}>' is missing property 'success' from type 'ng.IHttpPromise<any>'.  
like image 836
Alan2 Avatar asked Jul 26 '14 14:07

Alan2


People also ask

What is return type in TypeScript?

To define the return type for the function, we have to use the ':' symbol just after the parameter of the function and before the body of the function in TypeScript. The function body's return value should match with the function return type; otherwise, we will have a compile-time error in our code.

How do you find the return type of a function in TypeScript?

Use the ReturnType utility type to get the return type of a function in TypeScript, e.g. type T = ReturnType<typeof myFunction> . The ReturnType utility type constructs a type that consists of the return type of the provided function type.

How do you return an object in TypeScript?

To declare a function with an object return type, set the return type of the function to an object right after the function's parameter list, e.g. function getObj(): {name: string;} {} . If the return type of the function is not set, TypeScript will infer it.

What is angular IPromise?

IPromise , basically if you are using vs and as a suggestion when you hover over a particular method it will tell you what type it returns and you can derive the return type from that.

How to return a value from a function in typescript?

3. : return_type: This is the standard given by the TypeScript documentation to define the return type in TypeScript. We have to use the ‘:’ colon symbol to make this function return any value from it. Immediately after this, we can specify the type that we want to return from our function; it can be anything like string, number, or any, etc.

Why do we use ‘any’ type in typescript?

One advantage of using ‘any’ type in TypeScript is that we can return anything from our function then. By using that, our function is not specific to return number or string; rather, we can return anything from it. Now we will see one sample example for beginners to understand its implementation and usage see below;

What are the examples of typescript HTTP request?

Given below are the examples of TypeScript HTTP Request: TypeScript program to place a simple http GET request to a website by passing the URL of the website as the parameter to the fetch function and then converting the response from the website into a text format and printing it as the output the screen.

What are the arguments of a function in typescript?

In this example, sum is the name of the function, (a, b) are the arguments, and {return a + b;} is the function body. The syntax for creating functions in TypeScript is the same, except for one major addition: You can let the compiler know what types each argument or parameter should have.


1 Answers

Use

ng.IPromise<void>

Also you could let it implicitly type it for you if you don't declare a return type and don't use that interface.

Also there should be no return statement here :

return defer.resolve();

Just :

defer.resolve();
like image 193
basarat Avatar answered Sep 30 '22 04:09

basarat