betula/use-between

Typescript support or example?

nickdos opened this issue · 1 comments

I've implemented a custom hook that I can call via useBetween() in a normal JS component just fine but when I try calling it in a .tsx typescript component, like this:

  const { foo, bar } = useBetween(useCustomHook())

I get an error:

Property 'foo' does not exist on type 'unknown'.

I've tried to add types but it seems the useBetween() code is defining its own return type of (initialData?: any): unknown.

Edit: my customHook uses TS too with a defined return type (Object) - likely the issue?
Edit 2: Removing the return type declaration makes no difference to the error message.

I'm a TS noob so I might've missed something obvious - Is there a trick to getting it working in TS?

Hi @nickdos!

Your code example has a little syntax uncorrection.

  const { foo, bar } = useBetween(useCustomHook) // <-- no need to call custom hook here

useCustomHook will be called inside useBetween. One hook - one shared state! In other words, the hook is the identifier of the shared state.

Enjoy your code!