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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/pinia/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ export type _UnwrapAll<SS> = { [K in keyof SS]: UnwrapRef<SS[K]> }
export type _ExtractStateFromSetupStore<SS> = SS extends undefined | void
? {}
: _ExtractStateFromSetupStore_Keys<SS> extends keyof SS
? _UnwrapAll<Pick<SS, _ExtractStateFromSetupStore_Keys<SS>>>
? Pick<SS, _ExtractStateFromSetupStore_Keys<SS>>
: never

/**
Expand Down
13 changes: 12 additions & 1 deletion packages/pinia/test-dts/state.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { computed, ref, shallowRef } from 'vue'
import { computed, Ref, ref, shallowRef } from 'vue'
import { defineStore, expectType } from './'

const name = ref('Eduardo')
Expand All @@ -19,6 +19,7 @@ const useStore = defineStore({
counter,
aRef: ref(0),
aShallowRef: shallowRef({ msg: 'hi' }),
anotherShallowRef: shallowRef({ aRef: ref('hello') }),
}),

getters: {
Expand Down Expand Up @@ -67,6 +68,8 @@ expectType<number>(store.fromARef)

expectType<{ msg: string }>(store.aShallowRef)
expectType<{ msg: string }>(store.$state.aShallowRef)
expectType<{ aRef: Ref<string> }>(store.anotherShallowRef)
expectType<{ aRef: Ref<string> }>(store.$state.anotherShallowRef)
Comment on lines +71 to +72
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


const onlyState = defineStore({
id: 'main',
Expand All @@ -83,3 +86,11 @@ onlyState.$patch((state) => {
expectType<string>(state.some)
expectType<string>(state.name)
})

const useSetupStore = defineStore('composition', () => ({
anotherShallowRef: shallowRef({ aRef: ref('hello') }),
}))

const setupStore = useSetupStore()
expectType<{ aRef: Ref<string> }>(setupStore.anotherShallowRef)
expectType<{ aRef: Ref<string> }>(setupStore.$state.anotherShallowRef)
Comment on lines +95 to +96
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