Skip to content

Commit ea14e53

Browse files
authored
fix(types): unwrap refs in mapWritableState for setup stores (#2805)
Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com> Co-authored-by: Eduardo San Martin Morote <posva13@gmail.com> fix #2804
1 parent d1858e8 commit ea14e53

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

packages/pinia/src/mapHelpers.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ComponentPublicInstance, ComputedRef } from 'vue-demi'
1+
import type { ComponentPublicInstance, ComputedRef, UnwrapRef } from 'vue-demi'
22
import type {
33
_GettersTree,
44
_Method,
@@ -431,7 +431,7 @@ export function mapActions<
431431
/**
432432
* For internal use **only**
433433
*/
434-
export type _MapWritableStateReturn<S extends StateTree> = {
434+
export type _MapWritableStateReturn<S> = {
435435
[key in keyof S]: {
436436
get: () => S[key]
437437
set: (value: S[key]) => any
@@ -442,7 +442,7 @@ export type _MapWritableStateReturn<S extends StateTree> = {
442442
* For internal use **only**
443443
*/
444444
export type _MapWritableStateObjectReturn<
445-
S extends StateTree,
445+
S,
446446
T extends Record<string, keyof S>,
447447
> = {
448448
[key in keyof T]: {
@@ -464,11 +464,11 @@ export function mapWritableState<
464464
S extends StateTree,
465465
G extends _GettersTree<S>,
466466
A,
467-
KeyMapper extends Record<string, keyof S>,
467+
KeyMapper extends Record<string, keyof UnwrapRef<S>>,
468468
>(
469469
useStore: StoreDefinition<Id, S, G, A>,
470470
keyMapper: KeyMapper
471-
): _MapWritableStateObjectReturn<S, KeyMapper>
471+
): _MapWritableStateObjectReturn<UnwrapRef<S>, KeyMapper>
472472
/**
473473
* Allows using state and getters from one store without using the composition
474474
* API (`setup()`) by generating an object to be spread in the `computed` field
@@ -482,16 +482,11 @@ export function mapWritableState<
482482
S extends StateTree,
483483
G extends _GettersTree<S>,
484484
A,
485-
Keys extends keyof S,
485+
Keys extends keyof UnwrapRef<S>,
486486
>(
487487
useStore: StoreDefinition<Id, S, G, A>,
488488
keys: readonly Keys[]
489-
): {
490-
[K in Keys]: {
491-
get: () => S[K]
492-
set: (value: S[K]) => any
493-
}
494-
}
489+
): Pick<_MapWritableStateReturn<UnwrapRef<S>>, Keys>
495490
/**
496491
* Allows using state and getters from one store without using the composition
497492
* API (`setup()`) by generating an object to be spread in the `computed` field

packages/pinia/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { Pinia } from './rootStore'
1111
/**
1212
* Generic state of a Store
1313
*/
14-
export type StateTree = Record<string | number | symbol, any>
14+
export type StateTree = Record<PropertyKey, any>
1515

1616
export function isPlainObject<S extends StateTree>(
1717
value: S | unknown

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,20 @@ mapWritableState(useOptionsStore, ['upper'])
130130
// @ts-expect-error: cannot use a getter
131131
mapWritableState(useOptionsStore, { up: 'upper' })
132132

133+
expectType<{
134+
foo: {
135+
get: () => 'on' | 'off'
136+
set: (v: 'on' | 'off') => any
137+
}
138+
}>(mapWritableState(useSetupStore, { foo: 'a' }))
139+
140+
expectType<{
141+
a: {
142+
get: () => 'on' | 'off'
143+
set: (v: 'on' | 'off') => any
144+
}
145+
}>(mapWritableState(useSetupStore, ['a']))
146+
133147
const setupStoreWithState = mapState(useSetupStore, ['a'])
134148

135149
// store with no getters

0 commit comments

Comments
 (0)