Skip to content

Commit

Permalink
fix(types): Don't double UnwrapRef in setup stores
Browse files Browse the repository at this point in the history
fixes #2770
  • Loading branch information
bgoscinski committed Sep 11, 2024
1 parent 3c8782e commit 5519b8e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
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)

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)

0 comments on commit 5519b8e

Please sign in to comment.