zustand: Persist Partialize not working with TypeScript

I’ve been having trouble getting Zustand’s persist partialize middleware, to work with TypeScript. I’m not sure what type to provide and where.

Thanks in advance!

Here is the TS error I am currently getting.

Argument of type 'StateCreator<Store, [], [["zustand/persist", { siteId: string; }]], Store>' 
is not assignable to parameter of type' StateCreator<Store, [], [["zustand/persist", Store]], Store>'.

Type 'StateCreator<Store, [], [["zustand/persist", { siteId: string; }]], Store>' 
is not assignable to type '{ $$storeMutators?: [["zustand/persist", Store]]; }'.

Types of property '$$storeMutators' are incompatible.

Type '[["zustand/persist", { siteId: string; }]]' is not assignable to type '[["zustand/persist", Store]]'.

Type '["zustand/persist", { siteId: string; }]' is not assignable to type '["zustand/persist", Store]'.

Type at position 1 in source is not compatible with type at position 1 in target.

Type '{ siteId: string; }' is missing the following properties from type 'Store': tooltip, setTooltip, email, setEmail, and 5 more.

Here is my store in a codesandbox: https://codesandbox.io/s/flamboyant-sun-mwhr13?file=/store.ts

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (1 by maintainers)

Most upvoted comments

Hey @Fitzpa,

you are actually only missing one pair of () behind your call to create (your code const useStore = create<Store>(..)), so doing const useStore = create<Store>()(..) will work. You can also see that in the typescript example in the readme https://github.com/pmndrs/zustand#typescript-usage

Best

Wow! I looked at that example so many times and totally missed that parens every time. Haha. Yesterday I got this to work too. Though I’m not sure which is better. Your/the examples way seems way easier. Thank you so much!

const useStore = create<
  Store,
  [
    ['zustand/persist', { siteId: string; email: string; siteUrl: string }],
  ]
>

I tried using the same with the latest zustand 4.4.5, but running into the same error.

Typescript version is 5.0.2.

Here is the code:

import { create } from 'zustand';
import { persist } from 'zustand/middleware';

interface UserState {
	token: string;
	login: (token: string) => void;
}

export const useStore = create<UserState>()(
	persist(
		(set) => ({
			token: '',
			login: (token: string) => {
				set({ token });
			},
		}),
		{
			name: 'user-storage',
		},
	),
);

The error:

Argument of type 'StateCreator<UserState, [], [["zustand/persist", UserState]]>' is not assignable to parameter of type 'StateCreator<UserState, [], [never, unknown][]>'.
  Type 'StateCreator<UserState, [], [["zustand/persist", UserState]]>' is not assignable to type '{ $$storeMutators?: [never, unknown][] | undefined; }'.
    Types of property '$$storeMutators' are incompatible.
      Type '[["zustand/persist", UserState]] | undefined' is not assignable to type '[never, unknown][] | undefined'.
        Type '[["zustand/persist", UserState]]' is not assignable to type '[never, unknown][]'.
          Type '["zustand/persist", UserState]' is not assignable to type '[never, unknown]'.
            Type at position 0 in source is not compatible with type at position 0 in target.
              Type 'string' is not assignable to type 'never'.

See also #2163 We are working on it. Please use the previous version meanwhile. Sorry for the inconvenience.

I also found this article to be helpful where you create a persist type: https://cloverinks.medium.com/nextjs-zustand-typescript-type-conflict-with-persist-middleware-d72c2b3ec286

somebody create a new issue for this middleware typescript problem. please! zustand 4.4.5.

try to use v4.1.0, it is fine

I tried using the same with the latest zustand 4.4.5, but running into the same error.

Typescript version is 5.0.2.

Here is the code:

import { create } from 'zustand';
import { persist } from 'zustand/middleware';

interface UserState {
	token: string;
	login: (token: string) => void;
}

export const useStore = create<UserState>()(
	persist(
		(set) => ({
			token: '',
			login: (token: string) => {
				set({ token });
			},
		}),
		{
			name: 'user-storage',
		},
	),
);

The error:

Argument of type 'StateCreator<UserState, [], [["zustand/persist", UserState]]>' is not assignable to parameter of type 'StateCreator<UserState, [], [never, unknown][]>'.
  Type 'StateCreator<UserState, [], [["zustand/persist", UserState]]>' is not assignable to type '{ $$storeMutators?: [never, unknown][] | undefined; }'.
    Types of property '$$storeMutators' are incompatible.
      Type '[["zustand/persist", UserState]] | undefined' is not assignable to type '[never, unknown][] | undefined'.
        Type '[["zustand/persist", UserState]]' is not assignable to type '[never, unknown][]'.
          Type '["zustand/persist", UserState]' is not assignable to type '[never, unknown]'.
            Type at position 0 in source is not compatible with type at position 0 in target.
              Type 'string' is not assignable to type 'never'.

have a try with v4.1.0

I tried using the same with the latest zustand 4.4.5, but running into the same error.

Typescript version is 5.0.2.

Here is the code:

import { create } from 'zustand';
import { persist } from 'zustand/middleware';

interface UserState {
	token: string;
	login: (token: string) => void;
}

export const useStore = create<UserState>()(
	persist(
		(set) => ({
			token: '',
			login: (token: string) => {
				set({ token });
			},
		}),
		{
			name: 'user-storage',
		},
	),
);

The error:

Argument of type 'StateCreator<UserState, [], [["zustand/persist", UserState]]>' is not assignable to parameter of type 'StateCreator<UserState, [], [never, unknown][]>'.
  Type 'StateCreator<UserState, [], [["zustand/persist", UserState]]>' is not assignable to type '{ $$storeMutators?: [never, unknown][] | undefined; }'.
    Types of property '$$storeMutators' are incompatible.
      Type '[["zustand/persist", UserState]] | undefined' is not assignable to type '[never, unknown][] | undefined'.
        Type '[["zustand/persist", UserState]]' is not assignable to type '[never, unknown][]'.
          Type '["zustand/persist", UserState]' is not assignable to type '[never, unknown]'.
            Type at position 0 in source is not compatible with type at position 0 in target.
              Type 'string' is not assignable to type 'never'.

hi @dai-shi, can you help with this. im facing this error too. its happening while using persist and also immer for me.