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

fix(middleware): export PersistOptions type definitions #588

Merged
merged 3 commits into from
Oct 19, 2021

Conversation

abraaoz
Copy link
Contributor

@abraaoz abraaoz commented Oct 14, 2021

No description provided.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Oct 14, 2021

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:

Sandbox Source
React Configuration
React Typescript Configuration
React Browserify Configuration
React Snowpack Configuration
React Parcel Configuration
Next.js Configuration

@abraaoz abraaoz closed this Oct 14, 2021
@abraaoz abraaoz reopened this Oct 14, 2021
@dai-shi
Copy link
Member

dai-shi commented Oct 14, 2021

It's somewhat intentional to not export some of types.
What is your issue without exporting those?

@abraaoz
Copy link
Contributor Author

abraaoz commented Oct 15, 2021

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...
};

@dai-shi
Copy link
Member

dai-shi commented Oct 15, 2021

I must use the PersistOptions type from zustand/middleware in order to create my own persist middleware

That sounds reasonable. Can you make the PR to only export PersistOptions?

btw, this should be possible too:

type PersistOptions = Parameters<typeof persist>[1]

@abraaoz
Copy link
Contributor Author

abraaoz commented Oct 18, 2021

I must use the PersistOptions type from zustand/middleware in order to create my own persist middleware

That sounds reasonable. Can you make the PR to only export PersistOptions?

btw, this should be possible too:

type PersistOptions = Parameters<typeof persist>[1]

Using Parameters<> looses the Generic in the type definition.
Example:

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

@abraaoz
Copy link
Contributor Author

abraaoz commented Oct 18, 2021

"make the PR to only export PersistOptions"
done.

@dai-shi
Copy link
Member

dai-shi commented Oct 18, 2021

Using Parameters<> looses the Generic in the type definition.

Great point. I missed that.

Copy link
Member

@dai-shi dai-shi left a 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.

src/middleware.ts Outdated Show resolved Hide resolved
@dai-shi dai-shi merged commit e9a8eaa into pmndrs:main Oct 19, 2021
@dai-shi dai-shi changed the title chore: export middleware type definitions fix(middleware): export PersistOptions type definitions Oct 19, 2021
@abraaoz
Copy link
Contributor Author

abraaoz commented Oct 19, 2021

Thank you @dai-shi !!

@abraaoz abraaoz deleted the middleware-type-definitions branch October 19, 2021 15:41
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

Successfully merging this pull request may close these issues.

None yet

4 participants