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(utils): improve devtools typing to avoid depending on @redux-devtools/extension #777

Merged
merged 4 commits into from
Aug 12, 2023

Conversation

fukumasuya
Copy link
Contributor

@fukumasuya fukumasuya commented Aug 11, 2023

Related Issues or Discussions

Fixes #776

Summary

I found the same issue in zustand(pmndrs/zustand#1205 (comment)), so I added the same workaround(copied the type defs from @redux-devtools/extension)
I'm not sure if this way is a desired way, but I did it anyway..

Check List

  • yarn run prettier for formatting code and docs

@vercel
Copy link

vercel bot commented Aug 11, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
valtio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 12, 2023 10:29am

@codesandbox-ci
Copy link

codesandbox-ci bot commented Aug 11, 2023

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 3dc8f03:

Sandbox Source
React Configuration
React Typescript Configuration
React Browserify Configuration
React Snowpack Configuration
React Parcel Configuration

@fukumasuya
Copy link
Contributor Author

It might be a better solution to move @redux-devtools/extension from devDependencies to dependencies and import Config type from @redux-devtools/extension .. But it might seem a little odd that it's in dependencies and redux is a peerDependencies of @redux-devtools/extension and devtools imports redux, so valtio users need to install redux manually...

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.

So, it's basically doing the same as pmndrs/zustand#1207 ?
It looks okay then.

Comment on lines 13 to 15
type EnhancerOptions = Parameters<
NonNullable<(typeof window)['__REDUX_DEVTOOLS_EXTENSION__']>['connect']
>[0]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though, this looks better. Curious how this is failing?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see. We still need to install the package.
I wonder if we can type check on it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious why L124 doesn't cause a type error.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because it disappears, okay.

@dai-shi dai-shi changed the title fix(vanilla): copy devtools option types instead of importing fix(utils): improve devtools typing to avoid depending on @redux-devtools/extension Aug 12, 2023
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.

208d3c9 fixes #776 reproduction.

@fukumasuya
Copy link
Contributor Author

fukumasuya commented Aug 12, 2023

So, it's basically doing the same as pmndrs/zustand#1207 ?

Yes. that's right.

I also confirmed 208d3c9 fixes #776. thanks for updating my fix!

But it looks #713 feature does not work with TypeScript.. (tsc fails when we add some parameters that is coming from @redux-devtools/extensions.. is that ok for us?

const state = proxy({ count: 0, text: 'hello' });
devtools(state, { name: 'state name', enabled: true, latency: 1000 }); // 'latency' is coming from `@redux-devtools/extensions`
  Overload 1 of 2, '(proxyObject: { count: number; text: string; }, options?: { enabled?: boolean | undefined; name?: string | undefined; } | undefined): (() => void) | undefined', gave the following error.
    Argument of type '{ name: string; enabled: true; latency: number; }' is not assignable to parameter of type '{ enabled?: boolean | undefined; name?: string | undefined; }'.
      Object literal may only specify known properties, and 'latency' does not exist in type '{ enabled?: boolean | undefined; name?: string | undefined; }'.
  Overload 2 of 2, '(proxyObject: { count: number; text: string; }, name?: string | undefined): (() => void) | undefined', gave the following error.
    Argument of type '{ name: string; enabled: boolean; latency: number; }' is not assignable to parameter of type 'string'.

15 devtools(state, { name: 'state name', enabled: true, latency: 1000 });

@fukumasuya
Copy link
Contributor Author

It seems if we install @redux-devtools/extension and import some function in this or load the type def file, then __REDUX_DEVTOOLS_EXTENSION__ is declared and following code works without type errors.., In that case, we need to install redux additionally because redux is in peerDependencies of @redux-devtools/extension..

const state = proxy({ count: 0, text: 'hello' });
devtools(state, { name: 'state name', enabled: true, latency: 1000 }); // 'latency' is coming from `@redux-devtools/extensions`

tsconfig.json

{
  "include": [
    "./node_modules/@redux-devtools/extension/lib/types/index.d.ts",
  ]
}

@dai-shi
Copy link
Member

dai-shi commented Aug 12, 2023

Hmm, isn't it just enough to install @redux-devtools/extension?
In that case, we should update the docs to explicitly import it (both in valtio and zustand.)

import type {} from '@redux-devtools/extension'
import { devtools } from 'valtio/utlis'

@fukumasuya
Copy link
Contributor Author

Hmm, isn't it just enough to install @redux-devtools/extension?

It seems If we set skipLibCheck to true, we don't need to install redux.
Anyway, 208d3c9 fixes #776 and is fine for me 👍 thanks!

@dai-shi dai-shi merged commit d132d66 into pmndrs:main Aug 12, 2023
31 checks passed
@fukumasuya fukumasuya deleted the fix/issue-776 branch August 12, 2023 14:22
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.

tsc fails when using functions in valitio/utils
2 participants