Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pbts: Invalid type for callback argument of service method #582

Closed
osechet opened this issue Dec 22, 2016 · 1 comment
Closed

pbts: Invalid type for callback argument of service method #582

osechet opened this issue Dec 22, 2016 · 1 comment

Comments

@osechet
Copy link

osechet commented Dec 22, 2016

protobuf.js version: master

In the generated typescript definition file, the callback argument of service methods is defined as () => any. It makes it impossible to call the method with arguments.
The correct definition should be (err: Error | null, response?: <response_type>) => void (perfect) or Function (if too difficult to manage with tsd-jsdoc).

The same error applies for the rpc argument of the service's constructor that should be (method: Method, requestData: Uint8Array, callback: (err: Error | null, responseData?: Uint8Array) => void) => void (best) or (method: Function, requestData: Uint8Array, callback: Function) => void or Function.

With the greeter example:

syntax = "proto3";

message HelloRequest {
    string name = 1;
}
message HelloReply {
    string message = 1;
}
service Greeter {
    rpc SayHello (HelloRequest) returns (HelloReply) {}
}

we get:

declare module "greeter" {

    // ...

    /**
     * Constructs a new Greeter.
     * @exports Greeter
     * @constructor
     * @param {function(function, Uint8Array, function)} rpc RPC implementation
     * @param {boolean} [requestDelimited=false] Whether requests are length-delimited
     * @param {boolean} [responseDelimited=false] Whether responses are length-delimited
     */
    class Greeter {

        /**
         * Constructs a new Greeter.
         * @exports Greeter
         * @constructor
         * @param {function(function, Uint8Array, function)} rpc RPC implementation
         * @param {boolean} [requestDelimited=false] Whether requests are length-delimited
         * @param {boolean} [responseDelimited=false] Whether responses are length-delimited
         */
        constructor(rpc: () => any, requestDelimited?: boolean, responseDelimited?: boolean);

        /**
         * RPC implementation.
         * @type {function(function, Uint8Array, function)}
         */
        rpc: () => any;

        /**
         * Whether requests are length-delimited.
         * @type {boolean}
         */
        requestDelimited: boolean;

        /**
         * Whether responses are length-delimited.
         * @type {boolean}
         */
        responseDelimited: boolean;

        /**
         * Calls SayHello.
         * @param {HelloRequest|Object} request HelloRequest or plain object
         * @param {function(?Error, HelloReply=)} callback Node-style callback called with the error, if any, and HelloReply
         * @returns {undefined}
         */
        ["sayHello"](request: (HelloRequest|Object), callback: () => any): void;
    }
}

If I call sayHello(new HelloRequest(), (err, response) => {}), I get an error stating (err, response) => void is not assignable to () => any. If I call sayHello(..., () => {}), I cannot process the error or the response since no variable defines them.

@dcodeIO
Copy link
Member

dcodeIO commented Dec 27, 2016

Going to assume that the commit above resolved the issue. Feel free to reopen if it didn't!

@dcodeIO dcodeIO closed this as completed Dec 27, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants