hosseinmd / react-signalr

MIT License
43 stars 14 forks source link

Cannot create strongly typed hub #38

Closed heidgert closed 6 months ago

heidgert commented 7 months ago

Hi @hosseinmd,

Thanks for providing this library. It works great when using this approach:

const SignalRContext = createSignalRContext();

However, if I follow the example in the repo (with the Chat interface) like below it doesn't compile.

const SignalRContext = createSignalRContext<Chat>();

The error in VS Code looks like this:

Type 'Chat' does not satisfy the constraint 'Hub<string, string>'. Types of property 'callbacks' are incompatible. Type 'ChatCallbacks' is not assignable to type '{ [x: string]: <F extends (...args: any) => any>(...args: Parameters<F>) => void; }'. Property '[ChatCallbacksNames.hello]' is incompatible with index signature. Type '(message: string) => void' is not assignable to type '<F extends (...args: any) => any>(...args: Parameters<F>) => void'. Types of parameters 'message' and 'args' are incompatible. Type 'Parameters<F>' is not assignable to type '[message: string]'. Type 'any[]' is not assignable to type '[message: string]'. Target requires 1 element(s) but source may have fewer.´

I have pushed a minimal sample repo that shows the problem.

https://github.com/heidgert/react-signalr-lab/tree/main

Can you please guide me in the right direction on how to get this working?

Thanks!

heidgert commented 7 months ago

I noticed that cloning this repo and opening the sample file in VS Code gives the same error:

image

kiwikencox commented 6 months ago

Am seeing the same thing in our solution after updating some other packages. I did notice it was highlighted as an issue beforehand, but still build in production mode. Something must have gotten a bit stricter with the other packages we updated.

fdcastel commented 6 months ago

I'm getting a similar error after updating my project with the latest npm packages:

 error TS2345: Argument of type '(e: MyEvent) => void' is not assignable to parameter of type '<F extends (...args: any) => any>(...args: Parameters<F>) => void'.
  Types of parameters 'e' and 'args' are incompatible.
    Type 'Parameters<F>' is not assignable to type '[e: MyEvent]'.
      Type 'any[]' is not assignable to type '[e: MyEvent]'.
        Target requires 1 element(s) but source may have fewer.

Also, trying to build the latest main from https://github.com/hosseinmd/react-signalr with current npm packages is broken:

npm install

npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: react-signalr@0.2.19
npm ERR! Found: typescript@5.4.5
npm ERR! node_modules/typescript
npm ERR!   dev typescript@"^5.2.2" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer typescript@">=3.8 <5.0" from ts-jest@26.5.6
npm ERR! node_modules/ts-jest
npm ERR!   dev ts-jest@"^26.4.4" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.

@hosseinmd could you bring some light to this? 😉

fdcastel commented 6 months ago

Found the culprit: TypeScript 5.4

Pinning TypeScript to v5.3.3 with

"typescript": "5.3.3",

in packages.json fixed the problem.

fdcastel commented 6 months ago

BTW: @heidgert thank you for your sample!

Just passing to say that I had to add some missing dependencies:

npm i --save-dev @types/uuid
npm i --save-dev @types/js-cookie
npm i --save-dev @types/node

After that (and pinning TypeScript to 5.3.3 version) it builds fine.

hosseinmd commented 6 months ago

Fixed: v0.2.20

heidgert commented 5 months ago

Just tried this and it works well in v0.2.20. Thanks for the fix!