Skip to content

Commit fe6f21b

Browse files
committed
fix(types): unwrap refs in mapWritableState for setup stores
1 parent 6fb480e commit fe6f21b

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

packages/pinia/src/mapHelpers.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -465,10 +465,11 @@ export function mapWritableState<
465465
G extends _GettersTree<S>,
466466
A,
467467
KeyMapper extends Record<string, keyof S>,
468+
SS extends StateTree = Store<Id, S, G, A>,
468469
>(
469470
useStore: StoreDefinition<Id, S, G, A>,
470471
keyMapper: KeyMapper
471-
): _MapWritableStateObjectReturn<S, KeyMapper>
472+
): Pick<_MapWritableStateObjectReturn<SS, KeyMapper>, keyof KeyMapper>
472473
/**
473474
* Allows using state and getters from one store without using the composition
474475
* API (`setup()`) by generating an object to be spread in the `computed` field
@@ -483,15 +484,11 @@ export function mapWritableState<
483484
G extends _GettersTree<S>,
484485
A,
485486
Keys extends keyof S,
487+
SS extends StateTree = Store<Id, S, G, A>,
486488
>(
487489
useStore: StoreDefinition<Id, S, G, A>,
488490
keys: readonly Keys[]
489-
): {
490-
[K in Keys]: {
491-
get: () => S[K]
492-
set: (value: S[K]) => any
493-
}
494-
}
491+
): Pick<_MapWritableStateReturn<SS>, Keys>
495492
/**
496493
* Allows using state and getters from one store without using the composition
497494
* 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: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ expectType<{
116116
}
117117
}>(mapWritableState(useOptionsStore, ['a']))
118118
// @ts-expect-error: only defined in array
119-
mapWritableState(useStore, ['a']).b
119+
mapWritableState(useOptionsStore, ['a']).b
120120

121121
expectType<{
122122
newA: {
@@ -126,9 +126,23 @@ expectType<{
126126
}>(mapWritableState(useOptionsStore, { newA: 'a' }))
127127

128128
// @ts-expect-error: cannot use a getter
129-
mapWritableState(useStore, ['upper'])
129+
mapWritableState(useOptionsStore, ['upper'])
130130
// @ts-expect-error: cannot use a getter
131-
mapWritableState(useStore, { up: 'upper' })
131+
mapWritableState(useOptionsStore, { up: 'upper' })
132+
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']))
132146

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

0 commit comments

Comments
 (0)