-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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(middleware): export PersistOptions type definitions #588
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit b0fa90c:
|
It's somewhat intentional to not export some of types. |
I must use the PersistOptions type from zustand/middleware in order to create my own persist middleware, like this: import create, { State, StateCreator } from 'zustand';
import { persist, PersistOptions, StateStorage } from 'zustand/middleware';
export type Hydrated = {
/**
* Indicates whether the state has already been loaded from storage.
* @see https://github.com/pmndrs/zustand/issues/346#issuecomment-915819674
*/
hasHydrated?: boolean;
};
export type StateWithHydrated = State & Hydrated;
export type Migrations = {
[timestamp: number]: (previousState: object, previousVersion: number) => object
};
export const createWithPersist = <TState extends StateWithHydrated> (
createState: StateCreator<TState>,
optionsWithMigrations: PersistOptions<TState> & { migrations: Migrations },
) => {
// my implementation here...
}; |
That sounds reasonable. Can you make the PR to only export btw, this should be possible too: type PersistOptions = Parameters<typeof persist>[1] |
Using Parameters<> looses the Generic in the type definition. type PersistOptions = Parameters<typeof persist>[1];
const options: PersistOptions<TState> = ...; // ❌ does not work because it has generic
import { PersistOptions } from 'zustand/middleware';
const options: PersistOptions<TState> = ...; // ✔️ works, even with generic |
"make the PR to only export PersistOptions" |
Great point. I missed that. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please run prettier and it's good to go.
Thank you @dai-shi !! |
No description provided.