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

Typescript Compilation Error #25

Open
atyshka opened this issue May 20, 2022 · 4 comments
Open

Typescript Compilation Error #25

atyshka opened this issue May 20, 2022 · 4 comments

Comments

@atyshka
Copy link

atyshka commented May 20, 2022

Do you have a working compile-able example of the typescript interface? I attempted to implement the sample code like so:

import FurhatGUI, { Furhat } from "furhat-gui";
let furhat: Furhat;

function setupSubscriptions() {
  furhat.subscribe("com.myapp.MyCustomEvent", (event) => {
    console.log("received event: ", event.event_name);
  });
}

FurhatGUI()
  .then((connection) => {
    furhat = connection;
    furhat.onConnectionError((_connection: WebSocket, _ev: globalThis.Event) : any => {
      console.error("Error occured while connecting to Furhat skill");
    });
    furhat.onConnectionClose(() => {
      console.warn("Connection with Furhat skill has been closed");
    });
    setupSubscriptions();
  })
  .catch(console.error);

Typescript does not seem to like the callback signature, giving me the following error:

error TS2345: Argument of type '(_connection: WebSocket, _ev: globalThis.Event) => void' is not assignable to parameter of type '(this: WebSocket, ev: Event) => any'.

22 furhat.onConnectionError((_connection: WebSocket, _ev: globalThis.Event) => {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Of course I can remove the arguments and have a no-argument callback like with furhat.onConnectionClose, but I may need to use the event data. Any ideas why typescript is unhappy with this function signature?

@sinclabs
Copy link
Collaborator

sinclabs commented May 20, 2022

Hey @atyshka, did you try removing the any return type annotation in the callback function? The expected function signature for the callback has void as it's return type. NVM, that type coercion should fit I think. The problem might be the types of the other parameters. I am out of town right now and don't have my computer on me, I can check it out on Monday. In the meantime if you want also look into using furhat-core library maybe?

@atyshka
Copy link
Author

atyshka commented May 20, 2022

I get the same issue if I remove return type annotation:

    furhat.onConnectionError((_connection: WebSocket, _ev: globalThis.Event) => {
      console.error("Error occured while connecting to Furhat skill");
    });

produces

error TS2345: Argument of type '(_connection: WebSocket, _ev: globalThis.Event) => void' is not assignable to parameter of type '(this: WebSocket, ev: Event) => any'.

22     furhat.onConnectionError((_connection: WebSocket, _ev: globalThis.Event) => {
                                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I also thought it might be a problem with the input parameters so I tried changing the input types:

    furhat.onConnectionError((_connection: WebSocket, _ev: Event) => {
      console.error("Error occured while connecting to Furhat skill");
    });

but I get the same error

error TS2345: Argument of type '(_connection: WebSocket, _ev: Event) => void' is not assignable to parameter of type '(this: WebSocket, ev: Event) => any'.

22     furhat.onConnectionError((_connection: WebSocket, _ev: Event) => {

@atyshka
Copy link
Author

atyshka commented May 20, 2022

Hey @atyshka, did you try removing the any return type annotation in the callback function? The expected function signature for the callback has void as it's return type. NVM, that type coercion should fit I think. The problem might be the types of the other parameters. I am out of town right now and don't have my computer on me, I can check it out on Monday. In the meantime if you want also look into using furhat-core library maybe?

Ok no problem, it's not urgent, I appreciate the help. I'm not extensively versed in typescript, but I have gone through the core js library code and have not been able to find a solution. The official GUI sample app uses js instead of ts so that hasn't helped either. It's currently in a VueJS app right now, I'll try and see if I can get a minimal reproducible example we can both test

@atyshka
Copy link
Author

atyshka commented May 20, 2022

Oh, it appears that there should not be a websocket arg to the callback, only the event. So something like this:

furhat.onConnectionError((ev: Event) => {});

Because the websocket is bound to this and not exposed as an argument. Is that correct? If so I can submit a PR to amend the readme

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants