Skip to content

Commit

Permalink
fix(types): unwrap refs in mapWritableState for setup stores (#2805)
Browse files Browse the repository at this point in the history
Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com>
Co-authored-by: Eduardo San Martin Morote <posva13@gmail.com>

fix #2804
  • Loading branch information
jh-leong authored Nov 28, 2024
1 parent d1858e8 commit ea14e53
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
19 changes: 7 additions & 12 deletions packages/pinia/src/mapHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ComponentPublicInstance, ComputedRef } from 'vue-demi'
import type { ComponentPublicInstance, ComputedRef, UnwrapRef } from 'vue-demi'
import type {
_GettersTree,
_Method,
Expand Down Expand Up @@ -431,7 +431,7 @@ export function mapActions<
/**
* For internal use **only**
*/
export type _MapWritableStateReturn<S extends StateTree> = {
export type _MapWritableStateReturn<S> = {
[key in keyof S]: {
get: () => S[key]
set: (value: S[key]) => any
Expand All @@ -442,7 +442,7 @@ export type _MapWritableStateReturn<S extends StateTree> = {
* For internal use **only**
*/
export type _MapWritableStateObjectReturn<
S extends StateTree,
S,
T extends Record<string, keyof S>,
> = {
[key in keyof T]: {
Expand All @@ -464,11 +464,11 @@ export function mapWritableState<
S extends StateTree,
G extends _GettersTree<S>,
A,
KeyMapper extends Record<string, keyof S>,
KeyMapper extends Record<string, keyof UnwrapRef<S>>,
>(
useStore: StoreDefinition<Id, S, G, A>,
keyMapper: KeyMapper
): _MapWritableStateObjectReturn<S, KeyMapper>
): _MapWritableStateObjectReturn<UnwrapRef<S>, KeyMapper>
/**
* Allows using state and getters from one store without using the composition
* API (`setup()`) by generating an object to be spread in the `computed` field
Expand All @@ -482,16 +482,11 @@ export function mapWritableState<
S extends StateTree,
G extends _GettersTree<S>,
A,
Keys extends keyof S,
Keys extends keyof UnwrapRef<S>,
>(
useStore: StoreDefinition<Id, S, G, A>,
keys: readonly Keys[]
): {
[K in Keys]: {
get: () => S[K]
set: (value: S[K]) => any
}
}
): Pick<_MapWritableStateReturn<UnwrapRef<S>>, Keys>
/**
* Allows using state and getters from one store without using the composition
* API (`setup()`) by generating an object to be spread in the `computed` field
Expand Down
2 changes: 1 addition & 1 deletion packages/pinia/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Pinia } from './rootStore'
/**
* Generic state of a Store
*/
export type StateTree = Record<string | number | symbol, any>
export type StateTree = Record<PropertyKey, any>

export function isPlainObject<S extends StateTree>(
value: S | unknown
Expand Down
14 changes: 14 additions & 0 deletions packages/pinia/test-dts/mapHelpers.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,20 @@ mapWritableState(useOptionsStore, ['upper'])
// @ts-expect-error: cannot use a getter
mapWritableState(useOptionsStore, { up: 'upper' })

expectType<{
foo: {
get: () => 'on' | 'off'
set: (v: 'on' | 'off') => any
}
}>(mapWritableState(useSetupStore, { foo: 'a' }))

expectType<{
a: {
get: () => 'on' | 'off'
set: (v: 'on' | 'off') => any
}
}>(mapWritableState(useSetupStore, ['a']))

const setupStoreWithState = mapState(useSetupStore, ['a'])

// store with no getters
Expand Down

0 comments on commit ea14e53

Please sign in to comment.