Skip to content
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

SetState & GetState Typescript declaration in create function arguments (bis) #45

Closed
jfbrazeau opened this issue Jul 3, 2019 · 4 comments
Assignees

Comments

@jfbrazeau
Copy link

I opened an issue #32 a few weeks ago. The #35 pull request was supposed to fix the problem, but as far as I can see, it doesn't.

@jfbrazeau
Copy link
Author

jfbrazeau commented Jul 3, 2019

The fix works fine with a very basic sample like this one :
Screen Shot 2019-07-03 at 17 55 54

But I'm afraid that th fix is not enough because it is very easy to get a non working case. Imagine you have several stores, and imagine you want to do something generic on them. You will create a function that performs the "generic" things and then call that function for each store you have.

This will lead you to create a generic function that encapsulates the zustand create function invocation. But as far as I can see, it doesn't work.

Just take a look at this really trivial sample in which we try to do something generic during store intialization :
Screen Shot 2019-07-03 at 17 56 45

I hope you will agree with me that this sample code is trivial (10 lines of effective code), and it highlights the problem...

In other words, as soon as you want to add even a thin layer over zustand for your own purpose in Typescript world, you meet that problem.

I have put all this (trivial) code on codesandbox : https://codesandbox.io/s/j2nys
(basic.ts and initStore.ts files)

@jfbrazeau
Copy link
Author

Another way to present the problem is that today, it is not possible to extend zustand using typescript. Because anybody that would like to extend zustand will quickly have to create a generic function that encapsulates zustand createfunction.

And the keyof TState extends never part of the typescript definition prevents from being able to do that:

export default function create<TState extends State>(createState: keyof TState extends never ? StateCreator<State> : StateCreator<TState>): [UseStore<TState>, StoreApi<TState>];

@JeremyRH JeremyRH self-assigned this Jul 4, 2019
@JeremyRH
Copy link
Contributor

I can't find a way to allow extending create and keep inference working when not explicitly passing the type of State. I'll just allow inference to fail instead of trying to jump through hoops to get it working.

@jfbrazeau
Copy link
Author

#49 fixes this issue a far as I can see. I close the current issue. Thanks a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants