Zustand TypeScript Regression: Middleware Types Broken in v5.0.9 #3331
-
Bug DescriptionTypeScript middleware types are broken in Zustand v5.0.9. The same code that compiles correctly with Zustand v5.0.3 fails with complex type errors in v5.0.9. Environment
Reproduction🔗 TypeScript Playground: Live Reproduction Reproduction code (all 3 middleware patterns tested): import { create } from 'zustand';
import { devtools, subscribeWithSelector } from 'zustand/middleware';
interface Store {
count: number;
increment: () => void;
}
// ❌ TEST 1: devtools only - FAILS in v5.0.9
const useStoreDevtools = create<Store>()(
devtools(
(set, get) => ({
count: 0,
increment: () => set({ count: get().count + 1 }),
}),
{ name: 'DevtoolsStore' },
),
);
// ❌ TEST 2: subscribeWithSelector only - FAILS in v5.0.9
const useStoreSubscribe = create<Store>()(
subscribeWithSelector((set, get) => ({
count: 0,
increment: () => set({ count: get().count + 1 }),
})),
);
// ❌ TEST 3: Combined middleware - FAILS in v5.0.9
const useStoreCombined = create<Store>()(
devtools(
subscribeWithSelector((set, get) => ({
count: 0,
increment: () => set({ count: get().count + 1 }),
})),
{ name: 'CombinedStore' },
),
);
// All 3 tests pass in v5.0.3, all 3 fail in v5.0.9
export { useStoreCombined, useStoreDevtools, useStoreSubscribe };Steps to reproduce:
Verified locally: ✅ Error confirmed on [email protected], works on [email protected] Error MessageAdditional cascading errors: (For Expected BehaviorThe code should compile without errors, as it does in v5.0.3. Actual BehaviorTypeScript errors are thrown when using any middleware ( WorkaroundsWorkaround 1: Downgrade to v5.0.3 npm install [email protected]Workaround 2: Type assertion (not recommended) const useStore = (create as any)(
(devtools as any)(
(set, get) => ({...}),
{ name: 'Store' },
),
) as UseBoundStore<StoreApi<Store>>;Version Comparison (Verified Locally)
Test command used: # Install v5.0.9 and test
pnpm add [email protected]
npx tsc --noEmit # ❌ 6 errors
# Downgrade to v5.0.3 and test
pnpm add [email protected]
npx tsc --noEmit # ✅ 0 errorsAnalysisThe regression appears to be in the Diff of middleware/devtools.d.ts (5.0.3 vs 5.0.9): The type parameter naming changed slightly:
While this naming change seems innocuous, there may be other changes in the core type definitions affecting the Likely Cause: PR #3308Based on the closed PRs, the v5.0.9 release includes:
This PR added the new Suggested FixReview changes in PR #3308, specifically:
The Additional ContextThis was discovered while migrating a production application. The same codebase works with v5.0.3 (installed via Projects affected:
Reproduction Link |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
@parvez would you mind creating stackblitz demo? |
Beta Was this translation helpful? Give feedback.
NOT A BUG!
The error was caused by stale node_modules!
After a fresh pnpm install, the build passes with direct zustand imports
This was likely caused by:
Lesson learned: When encountering bizarre TypeScript errors, try rm -rf node_modules pnpm-lock.yaml && pnpm install first!