Skip to content

Commit

Permalink
fix(types): handle union types in generic parameter (#2794)
Browse files Browse the repository at this point in the history
Fix #2785
  • Loading branch information
jh-leong authored Oct 6, 2024
1 parent 2974e20 commit ecc7449
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
8 changes: 5 additions & 3 deletions packages/pinia/src/storeToRefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ type _ToStateRefs<SS> =
* Extracts the return type for `storeToRefs`.
* Will convert any `getters` into `ComputedRef`.
*/
export type StoreToRefs<SS extends StoreGeneric> = _ToStateRefs<SS> &
ToRefs<PiniaCustomStateProperties<StoreState<SS>>> &
_ToComputedRefs<StoreGetters<SS>>
export type StoreToRefs<SS extends StoreGeneric> = SS extends unknown
? _ToStateRefs<SS> &
ToRefs<PiniaCustomStateProperties<StoreState<SS>>> &
_ToComputedRefs<StoreGetters<SS>>
: never

/**
* Creates an object of references with all the state, getters, and plugin-added
Expand Down
11 changes: 10 additions & 1 deletion packages/pinia/test-dts/store.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -328,3 +328,12 @@ expectType<number>(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<Ref<string[]> | WritableComputedRef<'banana'[]>>(
storeToRefs(conditionalStore).bananas
)

0 comments on commit ecc7449

Please sign in to comment.