Comments (4)
#49 fixes this issue a far as I can see. I close the current issue. Thanks a lot.
from zustand.
The fix works fine with a very basic sample like this one :
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 :
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)
from zustand.
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 create
function.
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>];
from zustand.
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.
from zustand.
Related Issues (20)
- type WithoutCallSignature<T>
- docs: Add suggestions about getInitialState on persist
- Cant user Array of Map type to store data
- overview of the zustand API HOT 1
- Persist storage : values are converted to strings before replacer
- Value is empty on initial render and sets after few milliseconds
- Immer Middelware Typescript error: Exported variable 'useMyStore' has or is using name 'WritableDraft' from external module "../node_modules/immer/dist/immer" but cannot be named.
- blacklist and whitelist
- Argument of type '(state: ClientStore) => Client | undefined' is not assignable to parameter of type 'ClientStore | Partial<ClientStore> | ((state: ClientStore) => ClientStore | Partial<ClientStore>)'. Type '(state: ClientStore) => Client | undefined' is not assignable to type '(state: ClientStore) => ClientStore | Partial<ClientStore>'. Type 'Client | undefined' is not assignable to type 'ClientStore | Partial<ClientStore>'. Type 'undefined' is not assignable to type 'ClientStore | Partial<ClientStore>'.ts(2345)
- The value I save to storage with persist is delayed when the page refreshes.
- State updated in one component but not in another one
- Typescript + Slices = trouble HOT 7
- Setting nested state make the other states change (their values didn't change)
- zustand concurrent update issue
- Typo in GitHub "About" HOT 1
- import { create ,WritableDraft} from "zustand" HOT 1
- Introduction Zustand page a documentation typo HOT 1
- Ts error on type annotation HOT 3
- Persist middleware keeping old versions of functions around. Breaking apps when deploying to prod
- devtools with option `serialize.immutable` does not deserialize state from devtools correctly (broken redux devtools "Jump" / "Revert" etc.)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from zustand.