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

Merged
merged 1 commit into from
Sep 26, 2024
Merged

Conversation

bgoscinski
Copy link
Contributor

@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
Contributor 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
Contributor 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
Contributor Author

@posva this is ready for review :)

Copy link

pkg-pr-new bot commented Sep 26, 2024

Open in Stackblitz

pnpm add https://pkg.pr.new/@pinia/nuxt@2771
pnpm add https://pkg.pr.new/pinia@2771
pnpm add https://pkg.pr.new/@pinia/testing@2771

commit: 5519b8e

Copy link

codecov bot commented Sep 26, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.50%. Comparing base (3c8782e) to head (5519b8e).
Report is 2 commits behind head on v2.

Additional details and impacted files
@@            Coverage Diff             @@
##               v2    #2771      +/-   ##
==========================================
- Coverage   92.73%   92.50%   -0.23%     
==========================================
  Files          14       14              
  Lines        1335     1335              
  Branches      233      221      -12     
==========================================
- Hits         1238     1235       -3     
- Misses         97      100       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@posva posva left a comment

Choose a reason for hiding this comment

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

Thanks!

@posva posva merged commit 5ad1765 into vuejs:v2 Sep 26, 2024
8 of 9 checks passed
@yyx990803
Copy link
Member

FYI this breaks in ecosystem-ci with latest Vue: https://github.com/vuejs/ecosystem-ci/actions/runs/11050587708/job/30698660187

@posva
Copy link
Member

posva commented Sep 26, 2024

I see it when upgrading to latest vue. It seems to be related to the new syntax with getter and setter on Refs. I will have to check deeper and probably avoid the custom setter part for stores

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<...> }>
3 participants