From: Tycho Date: Sun, 6 Oct 2024 14:48:58 +0000 (+0800) Subject: fix(types): handle union types in generic parameter (#2794) X-Git-Tag: @pinia/nuxt@0.6.0~17 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ecc74492c642539ffc7386d57a8f9853437be327;p=thirdparty%2Fvuejs%2Fpinia.git fix(types): handle union types in generic parameter (#2794) Fix #2785 --- diff --git a/packages/pinia/src/storeToRefs.ts b/packages/pinia/src/storeToRefs.ts index 8d9cbe6e..5b268fc9 100644 --- a/packages/pinia/src/storeToRefs.ts +++ b/packages/pinia/src/storeToRefs.ts @@ -68,9 +68,11 @@ type _ToStateRefs = * Extracts the return type for `storeToRefs`. * Will convert any `getters` into `ComputedRef`. */ -export type StoreToRefs = _ToStateRefs & - ToRefs>> & - _ToComputedRefs> +export type StoreToRefs = SS extends unknown + ? _ToStateRefs & + ToRefs>> & + _ToComputedRefs> + : never /** * Creates an object of references with all the state, getters, and plugin-added diff --git a/packages/pinia/test-dts/store.test-d.ts b/packages/pinia/test-dts/store.test-d.ts index 3d90a66a..b8d9abde 100644 --- a/packages/pinia/test-dts/store.test-d.ts +++ b/packages/pinia/test-dts/store.test-d.ts @@ -5,7 +5,7 @@ import { expectType, storeToRefs, } from './' -import { computed, ref, UnwrapRef, watch } from 'vue' +import { computed, Ref, ref, UnwrapRef, watch, WritableComputedRef } from 'vue' const useStore = defineStore({ id: 'name', @@ -328,3 +328,12 @@ expectType(refs.bananasAmount.value) refs.bananasAmount.value = 0 // @ts-expect-error: this one is readonly refs.total.value = 0 + +const refStore = defineStore('ref-bananas', () => { + const bananas = ref(['banana1', 'banana2']) + return { bananas } +})() +declare const conditionalStore: typeof refStore | typeof writableComputedStore +expectType | WritableComputedRef<'banana'[]>>( + storeToRefs(conditionalStore).bananas +)