Skip to content

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

Closed
@osechet

Description

@osechet

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions