From eb0ebcdaf632b5ee1902245bf60a32df934bd6f0 Mon Sep 17 00:00:00 2001 From: Mikita Kamkou Date: Sat, 9 Mar 2024 13:18:57 +0100 Subject: [PATCH] fix(types): Added new mapped type to keep original ref type in the storeToRefs return type (close #2574) --- packages/pinia/src/storeToRefs.ts | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/packages/pinia/src/storeToRefs.ts b/packages/pinia/src/storeToRefs.ts index 76e91c333b..13b5e78161 100644 --- a/packages/pinia/src/storeToRefs.ts +++ b/packages/pinia/src/storeToRefs.ts @@ -11,7 +11,12 @@ import { toRefs, } from 'vue-demi' import { StoreGetters, StoreState } from './store' -import type { PiniaCustomStateProperties, StoreGeneric } from './types' +import type { + _UnwrapAll, + PiniaCustomStateProperties, + Store, + StoreGeneric, +} from './types' type ToComputedRefs = { [K in keyof T]: ToRef extends Ref @@ -19,13 +24,25 @@ type ToComputedRefs = { : ToRef } +/** + * 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. + */ +type ToStateRefs = + SS extends Store + ? UnwrappedState extends _UnwrapAll> + ? { + [K in Key]: State[K] extends Ref ? State[K] : Ref + } + : ToRefs + : ToRefs + /** * Extracts the return type for `storeToRefs`. * Will convert any `getters` into `ComputedRef`. */ -export type StoreToRefs = ToRefs< - StoreState & PiniaCustomStateProperties> -> & +export type StoreToRefs = ToStateRefs & + ToRefs>> & ToComputedRefs> /**