Home > Software engineering >  Typescript void is not assignable to parameter type ProcedureListener
Typescript void is not assignable to parameter type ProcedureListener

Time:04-07

I'm trying to call a function inside my class but I receive the following warning / error on this part:

rpc.register('cBrowser-SetUrl', (url: string, enableCursor: boolean) => {
    this.setBrowserUrl(url, enableCursor);
});

The error is the following: enter image description here

Full code:

const rpc = require('rage-rpc');

class BrowserSingleton {
    browser: BrowserMp;

    constructor() {
        this.browser = mp.browsers.new(' http://package/cef/index.html');

        rpc.register('cBrowser-SetUrl', (url: string, enableCursor: boolean) => {
            this.setBrowserUrl(url, enableCursor);
        });
    }

    setBrowserUrl(url: string, enableCursor: boolean) {
        const path = `app.$router.push('${url}');`;

        this.setInteractState(enableCursor);
        this.pasteJS(path);
    }

    pasteJS(data: string) {
        this.browser.execute(data);
    }

    setInteractState(state: boolean) {
        mp.gui.cursor.visible = state;
        mp.game.ui.displayRadar(!state);
        mp.gui.chat.show(!state);
        mp.nametags.enabled = !state;
    }
}

new BrowserSingleton();

Does anyone know why this happens? I appreciate any kind of help and suggestions.

CodePudding user response:

The callback function of rpc.register() has two parameters args and info. If you want to correctly call this function you need to declare the callback like this.

rpc.register('cBrowser-SetUrl', (args: any, info: ProcedureListenerInfo) => {
  this.setBrowserUrl(url, enableCursor);
});

The two arguments url and enableCursor are not available in the constructor yet. I suggest adding those to the constructor signature or put the register call inside the setBrowserURL function.

  • Related