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(types): Don't double UnwrapRef in setup stores #2771

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from

Conversation

bgoscinski
Copy link

@bgoscinski bgoscinski commented Sep 11, 2024

fixes #2770

Without this change for setup stores UnwrapRef is applied to state type twice.

First time after defineStore infers the SS type parameter and passes it to _ExtractStateFromSetupStore:

? _UnwrapAll<Pick<SS, _ExtractStateFromSetupStore_Keys<SS>>>

Then the result of _ExtractStateFromSetupStore<SS> is passed to StoreDefinition interface, which passes it to Store type, which does

UnwrapRef<S> &

First unwrap call reduces state type

// from
{ foo: ShallowRef<{ bar: Ref<string> }> }
// to 
{ foo: { bar: Ref<string> } }

Second unwrap call reduces it further producing type that doesn't match the runtime value

// from
{ foo: { bar: Ref<string> } }
// to the incorrect
{ foo: { bar: string } }

Copy link

netlify bot commented Sep 11, 2024

Deploy Preview for pinia-official canceled.

Name Link
🔨 Latest commit 5519b8e
🔍 Latest deploy log https://app.netlify.com/sites/pinia-official/deploys/66e21140e3257c000852dce9

Copy link

netlify bot commented Sep 11, 2024

Deploy Preview for pinia-playground ready!

Name Link
🔨 Latest commit 5519b8e
🔍 Latest deploy log https://app.netlify.com/sites/pinia-playground/deploys/66e21140309cde00085fd641
😎 Deploy Preview https://deploy-preview-2771--pinia-playground.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Comment on lines +71 to +72
expectType<{ aRef: Ref<string> }>(store.anotherShallowRef)
expectType<{ aRef: Ref<string> }>(store.$state.anotherShallowRef)
Copy link
Author

Choose a reason for hiding this comment

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

This was passing even without my change because types are correctly set when using option stores

Comment on lines +95 to +96
expectType<{ aRef: Ref<string> }>(setupStore.anotherShallowRef)
expectType<{ aRef: Ref<string> }>(setupStore.$state.anotherShallowRef)
Copy link
Author

Choose a reason for hiding this comment

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

my change causes that these 2 assertions pass as well

@bgoscinski
Copy link
Author

@posva this is ready for review :)

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.

Incorrect types for setup stores that contain ShallowRef<{ bar: Ref<...> }>
1 participant