react-signalr icon indicating copy to clipboard operation
react-signalr copied to clipboard

Cannot create strongly typed hub

Open heidgert opened this issue 3 months ago • 5 comments

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 avatar May 01 '24 17:05 heidgert