Skip to content

Commit

Permalink
refactor: Revert "refactor: Revert "fix(types): fix storeToRefs state…
Browse files Browse the repository at this point in the history
… return type (#2574) (#2604)""

This reverts commit f550c60.
  • Loading branch information
posva committed Apr 16, 2024
1 parent 877ed75 commit 48a2ca3
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 4 deletions.
34 changes: 30 additions & 4 deletions packages/pinia/src/storeToRefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,47 @@ import {
toRefs,
} from 'vue-demi'
import { StoreGetters, StoreState } from './store'
import type { PiniaCustomStateProperties, StoreGeneric } from './types'
import type {
_ActionsTree,
_GettersTree,
_UnwrapAll,
PiniaCustomStateProperties,
StateTree,
Store,
StoreGeneric,
} from './types'

type ToComputedRefs<T> = {
[K in keyof T]: ToRef<T[K]> extends Ref<infer U>
? ComputedRef<U>
: ToRef<T[K]>
}

/**
* Extracts the refs of a state object from a store. If the state value is a Ref or type that extends ref, it will be kept as is.
* Otherwise, it will be converted into a Ref.
* @internal
*/
type ToStateRefs<SS> =
SS extends Store<
string,
infer UnwrappedState,
_GettersTree<StateTree>,
_ActionsTree
>
? UnwrappedState extends _UnwrapAll<Pick<infer State, infer Key>>
? {
[K in Key]: ToRef<State[K]>
}
: ToRefs<UnwrappedState>
: ToRefs<StoreState<SS>>

/**
* Extracts the return type for `storeToRefs`.
* Will convert any `getters` into `ComputedRef`.
*/
export type StoreToRefs<SS extends StoreGeneric> = ToRefs<
StoreState<SS> & PiniaCustomStateProperties<StoreState<SS>>
> &
export type StoreToRefs<SS extends StoreGeneric> = ToStateRefs<SS> &
ToRefs<PiniaCustomStateProperties<StoreState<SS>>> &
ToComputedRefs<StoreGetters<SS>>

/**
Expand Down
73 changes: 73 additions & 0 deletions packages/pinia/test-dts/customizations.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,76 @@ expectType<{
})()
)
)

expectType<{
n: Ref<number>
customN: Ref<number> & { plusOne: () => void }
double: ComputedRef<number>
myState: Ref<number>
stateOnly: Ref<number>
}>(
storeToRefs(
defineStore('a', () => {
const n = ref(1)
const customN = ref(1) as Ref<number> & { plusOne: () => void }
const double = computed(() => n.value * 2)
return {
n,
customN,
double,
}
})()
)
)

expectType<{
n: Ref<number>
customN: Ref<number> & { plusOne: () => void }
double: ComputedRef<number>
myState: Ref<number>
stateOnly: Ref<number>
}>(
storeToRefs(
defineStore('a', () => {
const n = ref(1)
const customN = ref(1) as Ref<number> & { plusOne: () => void }
const double = computed(() => n.value * 2)

function plusOne() {
customN.value++
}

return {
n,
customN,
double,
plusOne,
}
})()
)
)

expectType<{
n: Ref<number>
customN: Ref<number> & { plusOne: () => void }
double: ComputedRef<number>
myState: Ref<number>
stateOnly: Ref<number>
}>(
storeToRefs(
defineStore('a', {
state: () => ({
n: 1,
customN: ref(1) as Ref<number> & { plusOne: () => void },
}),
getters: {
double: (state) => state.n * 2,
},
actions: {
plusOne() {
this.n++
},
},
})()
)
)

0 comments on commit 48a2ca3

Please sign in to comment.