Skip to content

Commit ecc7449

Browse files
authored
fix(types): handle union types in generic parameter (#2794)
Fix #2785
1 parent 2974e20 commit ecc7449

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

packages/pinia/src/storeToRefs.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,11 @@ type _ToStateRefs<SS> =
6868
* Extracts the return type for `storeToRefs`.
6969
* Will convert any `getters` into `ComputedRef`.
7070
*/
71-
export type StoreToRefs<SS extends StoreGeneric> = _ToStateRefs<SS> &
72-
ToRefs<PiniaCustomStateProperties<StoreState<SS>>> &
73-
_ToComputedRefs<StoreGetters<SS>>
71+
export type StoreToRefs<SS extends StoreGeneric> = SS extends unknown
72+
? _ToStateRefs<SS> &
73+
ToRefs<PiniaCustomStateProperties<StoreState<SS>>> &
74+
_ToComputedRefs<StoreGetters<SS>>
75+
: never
7476

7577
/**
7678
* Creates an object of references with all the state, getters, and plugin-added

packages/pinia/test-dts/store.test-d.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
expectType,
66
storeToRefs,
77
} from './'
8-
import { computed, ref, UnwrapRef, watch } from 'vue'
8+
import { computed, Ref, ref, UnwrapRef, watch, WritableComputedRef } from 'vue'
99

1010
const useStore = defineStore({
1111
id: 'name',
@@ -328,3 +328,12 @@ expectType<number>(refs.bananasAmount.value)
328328
refs.bananasAmount.value = 0
329329
// @ts-expect-error: this one is readonly
330330
refs.total.value = 0
331+
332+
const refStore = defineStore('ref-bananas', () => {
333+
const bananas = ref(['banana1', 'banana2'])
334+
return { bananas }
335+
})()
336+
declare const conditionalStore: typeof refStore | typeof writableComputedStore
337+
expectType<Ref<string[]> | WritableComputedRef<'banana'[]>>(
338+
storeToRefs(conditionalStore).bananas
339+
)

0 commit comments

Comments
 (0)