-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
TS issue with the set function when using devtools (with older TS versions) #1013
Comments
Is it TypeScript issue? or runtime issue? |
Are you asking me? I think it is a TypeScript issue at build time. |
If it's a TS issue at build time, does it go away if you remove but, tbh, I'm not sure how TS error can only happen for production build. |
@dai-shi yes if we remove I can try and use the enabled flag and see what that does. Let me get back to you. |
Is this development mode? not production build?
Oh, it's a different error from the OP one. |
I'm having the same issue as @hasan-almujtaba as well. But I don't get this issue when experimenting on small repo. I'm getting the error on other large project when trying to upgrade Zustand to v4. The issue is Typescript is expecting the required third |
Thanks for fast response @dai-shi, i'm using your awesome library (Zustand) in this repo : https://github.com/hasan-Almujtaba/next-starter . It's looks like it just a TS typing problem because it's work fine in redux devtools. |
@dai-shi Thanks for your help so far, really appreciate it. So, I tried the enabled flag but that didn't work, and the build still fails. Instead I tried to remove the parentheses directly after the create func (the opposite of what the TS docs say) and that gives me a new error (probably because I'm using zustand v4):
So then I tried to use this solution and gave some extra generic types like this: Type error: Expression produces a union type that is too complex to represent.
50 | [['zustand/devtools', IBookingBarState]]
51 | >(
> 52 | devtools(
| ^
53 | (set) => ({
54 | view: null,
55 | damageType: null, The setup I'm using is a Next.js app running inside an NX monorepo and so it is the Next.js build that is failing at build time. Running in dev mode works 100% of the time. I made a quick demo that seems to reproduce the same problem and as @hasan-almujtaba suggests, it might just be the typings are missing some partials. https://stackblitz.com/edit/nextjs-wjmkub?file=pages%2Findex.tsx Using Hope this helps you investigate the issue. |
Okay, I think I assumed the issue totally wrong. I thought it's only production build issue, but it seems just a TS typing issue. I don't know where https://codesandbox.io/s/wild-browser-bj8x83?file=/src/App.tsx works in csb. (@hasan-almujtaba your issue seems different, please try to have your function inline.) @devanshj Can you please help this? |
@dai-shi it's works fine if i have my function inline, but it's give me error when i'm trying to split my store to separate slices. I think the problem here is in the Also if i look to https://github.com/pmndrs/zustand/blob/main/src/vanilla.ts on line 16 - 20, there's only 2 parameter defined where actually you need third parameter for defining actionType name. |
@hasan-almujtaba Would you please open a new discussion? Or maybe add comments in #976? #980 is also related. This issue focuses on the problem even with inline functions. |
It's a typescript issue, the type type SetState<T extends State> = {
_(partial: T | Partial<T> | ((state: T) => T | Partial<T>), replace?: boolean | undefined, actionName?: string): void;
}['_']; |
The issue is reproducible in this stackblitz as said above, but not reproducible in this typescript playground with the same code. That means zustand types are correct, it's NextJS that is doing something funny. Maybe the So the way you'd debug is the way you debug all bugs—by binary search. What you'd want to do is start with a fresh folder, install If anyone here can't do this I'll do it if and when I have the time for it. But one thing that is 99% certain is that there's no problem in zustand types. |
@devanshj thanks for the help ❤️ I found the issue and it was our TS version. Seems the version 4.6.4 and above works (your demo was using the latest stable 4.7.4) but our repo was using 4.3.5. Downgrading your demo caused the same issue. Updating our TS dependency solves the issue. |
Ah I see, glad I was of help! It would be nice if we could document the minimum ts version required. Perhaps someone could confirm it by writing a script that might look something like this (perhaps a bash version of it)... import { $ } from "zx"
let v = 4.7
while (v >= 2.0) {
await $`yarn install typescript@${v}`
try {
await $`yarn tsc --noEmit`
console.log(`${v} compiles`)
} catch {
console.log(`${v} doesn't compile`)
console.log(`---`)
console.log(`Minimum version requirement is ${v + 0.1}`)
break
}
v -= 0.1
} One could even write a more sophisticated script that also retries compiling after downleveling (with |
So, we don't use the new features of TS, but we rely on behaviors of new TS. It's unfortunate that we can't support old TS (as zustand is trying to be "stable" compared to the other two), but can't be helped. |
Yeah although I'm quite surprised that the types are leveraging behaviors that are so recent that even something like 4.3 doesn't compile. Usually it's not a big deal to update typescript to latest version because typescript has a goal of being mostly non-breaking—more strictness and consequent breakage is opt-in via flags. I think the reason that old versions don't compile could also be rather stupid like old versions not being able to recognise named tuples (ie |
Haha, if you didn't know, we didn't know.
You are too optimistic about it. TypeScript package can be installed by a framework, and (beginner) devs don't even touch it. Anyway, I agree that we should know the minimum requirement (with/out downlevel-dts). Would anyone be willing to help? |
@devanshj My gut feeling is zustand/src/middleware/devtools.ts Lines 27 to 45 in 3d764b0
Yeah, this is where If this is the only blocker, we might be able to consider changing JS implementation to avoid |
I get that but what I meant was once the user knows old version is the problem, all they have to do is run
We could do that in that case we won't need But I think we should try to solve it starting from the most direct solution—downleveling. Because today it's |
I'm running into the same error, but I'm using typescript 4.7.4, which according to a comment above, should work? my tsconfig:
|
Have the same issue as @naps62 after upgrading to v4, type StoredPages = { pages: RecentPage[]; setPages: (pages: RecentPage[]) => void }
export const useStoredPages = create<StoredPages>(
persist(
(set) => ({
pages: [],
setPages: (pages: RecentPage[]) => set(() => ({ pages })),
}),
{ name: RECENT_PAGES_KEY},
),
)
|
Are you sure using TypeScript 4.7.4? |
@JClackett forgot to update here, but after some more searching I found the problem on another issue here that I can't recall you should actually do const store = create<StoredPages>()(
// ...
) notice the extra parenthesis |
Nice! Thanks! - functionally still works without the extra parenthesis? Is that right |
As https://tsplay.dev/N5LjVw shows, TS v4.4.4 causes an error. |
Yes this resolved the problem for me too: export const useStoreState = create()( |
Why is this not in the documentation? Or am I missing it? |
I think #1348 solved the original issue. If you still have some issue, please open a new discussion. |
Hey there zustand team
I'm having a problem when using zustand with devtools in my next.js project.
When running the store locally, everything works fine and I'm seeing devtools. Awesome.
But, when building my app for production with nx/next.js, it throws the following error:
What have I do so far:
tests/middlewareTypes.test.tsx
andtests/devtools.test.tsx
examples but not successful.create<InitialState, [["zustand/devtools", InitialState]]>
Using the following core dependencies:
Let me know if you need more details or if this makes no sense.
The text was updated successfully, but these errors were encountered: