Skip to content

Callback parameter type not inferred from RpcStub (implicitly any) #134

@VastBlast

Description

@VastBlast

Been testing out this library -- awesome work. I noticed when calling an RPC method that accepts an RpcStub with a callback, the callback parameter type is not inferred and defaults to any. When explicitly adding the type, it works fine, and also (correctly) gives you a type error if you put the wrong type. But when you leave it empty, it defaults to any.

See the example below for what I mean:

import { newWebSocketRpcSession, RpcStub, RpcTarget } from 'capnweb'

class MyRpcServer extends RpcTarget {
    sendMeCurrentTs(callback: RpcStub<(ts: number) => void>) {
        callback(Date.now());
    }
}

const stub = newWebSocketRpcSession<MyRpcServer>('ws://example.com');

stub.sendMeCurrentTs((ts) => { // Parameter 'ts' implicitly has an 'any' type. Why isn't it being inferred as number?
    console.log('received ts:', ts);
});

// BELOW WORKS AS EXPECTED

stub.sendMeCurrentTs((ts: string) => { // type error (as expected, ts should be a number)
    console.log('received ts:', ts);
});

stub.sendMeCurrentTs((ts: number) => { // no type issues, works as expected
    console.log('received ts:', ts);
});

I do not believe this is the same problem as #112 since I am using "lib": ["ESNext"] in my tsconfig. I also tested and did not have the issue mentioned there.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions