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
Fix SetState & GetState Typescript declaration in create function arg… #31
Conversation
…uments (replace State by TState)
This is similar to a PR I had opened (#27) but I believe this change breaks autocompletion for non typescript users. I think the workaround is to do
|
@JeremyRH @Magellol @jfbrazeau could we discuss this again? We just ran into it as well. Is it important if non TS users get broken intellisense? TBH, as a non TS use im kinda used to nothing giving me autocomplete. But now that i work on a TS project, passing on set/get into a factory, it's a little annoying the way it is and this PR would help it. |
I'm afraid I don't understand your workaround. I've tried it to see what it gives and here is the result : As you can see I've described a very simple state ( The (very unsatisfying) workaround I found consists in duplicating the zustand defintion file (
becomes in my environment this one :
When you are a Typescript user, the default zustand definition is very strange. The |
Could some kind of function overloading solve the problem? Or the definition of a second, typed function to create a store? export function createT<TState extends State>(
createState: (set: SetState<TState>, get: GetState<TState>, api: any) => TState
): [UseStore<TState>, StoreApi<TState>] {
return create<TState>(createState as any);
} Such a "wrapper" function could even be implemented in the application code, so you would not have to cast every time. |
closed in favour of #35 |
The
create
function typescript definition has a generic parameter that helps to define the final state that will be used :TState extends State
. This state has to extendState
(Record<string, any>
) which is relevant for a state.Unfortunately both
set
andget
parameters types don't use this generic parameter and instead are bound toState
as we can see :createState: (set: SetState<State>, get: GetState<State>, api: any) => TState
It is not very anoying for the
set
parameter, but it is really a pity that when one uses theget
method, the definition does not simply type the return toTState
instead of the very neutralState
.Is it just a mistake ? Or is there a reason not to have used
TState
inset: SetState<State>
andget: GetState<State>
? (I must admit that I would expectset: SetState<TState>
andget: GetState<TState>
instead).