chore: improve typing in devtools middleware#3362
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
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. |
dai-shi
left a comment
There was a problem hiding this comment.
If we were to do api as typeof api & WithDispatch, should WithDispatch have optional properties?
| } | ||
|
|
||
| type WithDispatch = { | ||
| dispatch: (...args: any[]) => any |
There was a problem hiding this comment.
Updated
| (fn, devtoolsOptions = {}) => | ||
| (set, get, api) => { | ||
| const { enabled, anonymousActionType, store, ...options } = devtoolsOptions | ||
| const apiWithDispatch = api as typeof api & WithDispatch |
There was a problem hiding this comment.
This increases bundle size. We hope only type improvements without affecting the result of bundling.
There was a problem hiding this comment.
Removed this in favor of type guard. Now bundle size became smaller than before.
|
Hello @dai-shi.
You right, but I already removed this line for other reason. |
dai-shi
left a comment
There was a problem hiding this comment.
Nice. The type guard approach is more reasonable.
| dispatchFromDevtools: true | ||
| } | ||
|
|
||
| const hasDispatch = (api: unknown): api is WithDispatch => |
There was a problem hiding this comment.
Actually, the name is confusing. This is not to check if it has the function, but if devtools should dispatch.
| const hasDispatch = (api: unknown): api is WithDispatch => | |
| const shouldDispatchFromUs = (api: unknown): api is WithDispatch => |
There was a problem hiding this comment.
Done, but the name is slightly different.
| typeof (api as WithDispatch).dispatch === 'function' && | ||
| (api as WithDispatch).dispatchFromDevtools |
There was a problem hiding this comment.
Let's keep the original order:
| typeof (api as WithDispatch).dispatch === 'function' && | |
| (api as WithDispatch).dispatchFromDevtools | |
| (api as WithDispatch).dispatchFromDevtools && | |
| typeof (api as WithDispatch).dispatch === 'function' |
|
|
||
| type WithDispatch = { | ||
| dispatch: (...args: unknown[]) => void | ||
| dispatchFromDevtools: true |
There was a problem hiding this comment.
| dispatchFromDevtools: true | |
| dispatchFromDevtools: unknown |
should be more precise.
| } | ||
|
|
||
| const shouldDispatchFromDevtools = (api: unknown): api is WithDispatch => | ||
| (api as WithDispatch).dispatchFromDevtools === true && |
There was a problem hiding this comment.
Let's not change the logic.
| (api as WithDispatch).dispatchFromDevtools === true && | |
| (api as WithDispatch).dispatchFromDevtools && |
There was a problem hiding this comment.
Sorry, but this is required since dispatchFromDevtools is unknown now. The intent of shouldDispatchFromDevtools not only check if fields are existing, but that dispatchFromDevtools is true. If this is not done it won't fully substitute original behavior.
Also, leaving (api as WithDispatch).dispatchFromDevtools intact throws a TS error:
Type unknown is not assignable to type boolean
There was a problem hiding this comment.
We didn't check if it's true, only if it's truthy. That's the original behavior. Do some workaround for the TS error. You can move it back to true from unknown if there's no workaround.
There was a problem hiding this comment.
Updated. I picked !! over Boolean() because double negation already has been used in the repository.
There was a problem hiding this comment.
Ah, I see. It was because of the type guard.
5651230 to
634f047
Compare
dai-shi
left a comment
There was a problem hiding this comment.
LGTM
Thanks for your contribution!
|
Thank you for guiding me through this. |
Bumps [zustand](https://github.com/pmndrs/zustand) from 5.0.10 to 5.0.11. Release notes *Sourced from [zustand's releases](https://github.com/pmndrs/zustand/releases).* > v5.0.11 > ------- > > This release includes small improvements in middleware thanks to contributors. > > What's Changed > -------------- > > * chore: improve typing in devtools middleware by [`@grigoriy-reshetniak`](https://github.com/grigoriy-reshetniak) in [pmndrs/zustand#3362](https://redirect.github.com/pmndrs/zustand/pull/3362) > * fix(persist): avoid relying on global localStorage by [`@honuuk`](https://github.com/honuuk) in [pmndrs/zustand#3367](https://redirect.github.com/pmndrs/zustand/pull/3367) > * fix(immer): Proper typing for immer middleware in combination with slices by [`@wheerd`](https://github.com/wheerd) in [pmndrs/zustand#3371](https://redirect.github.com/pmndrs/zustand/pull/3371) > > New Contributors > ---------------- > > * [`@SeongYongLee`](https://github.com/SeongYongLee) made their first contribution in [pmndrs/zustand#3355](https://redirect.github.com/pmndrs/zustand/pull/3355) > * [`@grigoriy-reshetniak`](https://github.com/grigoriy-reshetniak) made their first contribution in [pmndrs/zustand#3351](https://redirect.github.com/pmndrs/zustand/pull/3351) > * [`@DormancyWang`](https://github.com/DormancyWang) made their first contribution in [pmndrs/zustand#3363](https://redirect.github.com/pmndrs/zustand/pull/3363) > * [`@Ea-st-ring`](https://github.com/Ea-st-ring) made their first contribution in [pmndrs/zustand#3369](https://redirect.github.com/pmndrs/zustand/pull/3369) > * [`@winner07`](https://github.com/winner07) made their first contribution in [pmndrs/zustand#3373](https://redirect.github.com/pmndrs/zustand/pull/3373) > * [`@honuuk`](https://github.com/honuuk) made their first contribution in [pmndrs/zustand#3367](https://redirect.github.com/pmndrs/zustand/pull/3367) > * [`@wheerd`](https://github.com/wheerd) made their first contribution in [pmndrs/zustand#3371](https://redirect.github.com/pmndrs/zustand/pull/3371) > > **Full Changelog**: <pmndrs/zustand@v5.0.10...v5.0.11> Commits * [`99379a6`](pmndrs/zustand@99379a6) 5.0.11 * [`c81b4eb`](pmndrs/zustand@c81b4eb) chore(deps): update dev dependencies ([#3375](https://redirect.github.com/pmndrs/zustand/issues/3375)) * [`3871d53`](pmndrs/zustand@3871d53) fix(immer): Proper typing for immer middleware in combination with slices (#... * [`9b505ac`](pmndrs/zustand@9b505ac) fix(persist): use window.localStorage as default storage reference ([#3367](https://redirect.github.com/pmndrs/zustand/issues/3367)) * [`267a57c`](pmndrs/zustand@267a57c) Update code block in tutorial-tic-tac-toe.md ([#3373](https://redirect.github.com/pmndrs/zustand/issues/3373)) * [`6813f7b`](pmndrs/zustand@6813f7b) docs: remove stray Russian comment in beginner-typescript guide ([#3369](https://redirect.github.com/pmndrs/zustand/issues/3369)) * [`d9ea330`](pmndrs/zustand@d9ea330) docs(testing): fix undefined counterStoreRef variable ([#3368](https://redirect.github.com/pmndrs/zustand/issues/3368)) * [`6e026d7`](pmndrs/zustand@6e026d7) chore: improve typing in devtools middleware ([#3362](https://redirect.github.com/pmndrs/zustand/issues/3362)) * [`e7d4593`](pmndrs/zustand@e7d4593) Revert "chore(deps): bump pmndrs/docs/.github/workflows/build.yml from 2 to 3... * [`0f49ad8`](pmndrs/zustand@0f49ad8) chore(deps): bump pmndrs/docs/.github/workflows/build.yml from 2 to 3 ([#3364](https://redirect.github.com/pmndrs/zustand/issues/3364)) * Additional commits viewable in [compare view](pmndrs/zustand@v5.0.10...v5.0.11) [](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores) Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. [//]: # (dependabot-automerge-start) [//]: # (dependabot-automerge-end) --- Dependabot commands and options You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot merge` will merge this PR after your CI passes on it - `@dependabot squash and merge` will squash and merge this PR after your CI passes on it - `@dependabot cancel merge` will cancel a previously requested merge and block automerging - `@dependabot reopen` will reopen this PR if it is closed - `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually - `@dependabot show ignore conditions` will show all of the ignore conditions of the specified dependency - `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
Summary
While going after another FIXME comment I've noticed repetitive
api as anyin devtools middleware. Also fixed someas anyon the way.Check List
pnpm run fixfor formatting and linting code and docsnpx tsc --noEmitto check ts errors