Generic typing for the useStore selector (the first parameter of the hook) #5562
LLloKoLaDKa
started this conversation in
General
Replies: 1 comment
-
|
@moklick Any thoughts on this? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello, team!
I found a small problem with the typing of the
useStorehook and would like to discuss possible ways to solve it — perhaps this will be a reason to improve the library.The essence of the problem:
I use custom types for nodes and edges (
MapNode,MapEdge) and want to write selectors that work with these types directly. For example:However, this approach causes a typing error in the useStore, because its signature is rigidly tied to ReactFlowState without generics:
As a result, you either have to use as-type conversion inside the selector (which is not very elegant), or abandon strict typing.
This is what my use of useStore and selectors looks like now (please pay attention to
(store.nodes as MapNode[])):Offer:
It would be great if useStore supported generic types for ReactFlowState, for example:
This would allow you to create convenient wrappers like:
const useMapStore = <T>(selector: (state: ReactFlowState<MapNode, MapEdge>) => T) => useStore(selector);...and would eliminate the need to cast types in each selector.
If the current behavior is a deliberate limitation (for example, for reasons of backward compatibility or architecture), it would be useful to know about it. Otherwise, perhaps it is worth considering such a revision?
Thank you for the excellent library and your attention!
Beta Was this translation helpful? Give feedback.
All reactions