Skip to content

Commit

Permalink
fix(types): unwrap refs in mapWritableState for setup stores
Browse files Browse the repository at this point in the history
  • Loading branch information
jh-leong committed Oct 25, 2024
1 parent 6fb480e commit fe6f21b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
11 changes: 4 additions & 7 deletions packages/pinia/src/mapHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,10 +465,11 @@ export function mapWritableState<
G extends _GettersTree<S>,
A,
KeyMapper extends Record<string, keyof S>,
SS extends StateTree = Store<Id, S, G, A>,
>(
useStore: StoreDefinition<Id, S, G, A>,
keyMapper: KeyMapper
): _MapWritableStateObjectReturn<S, KeyMapper>
): Pick<_MapWritableStateObjectReturn<SS, KeyMapper>, keyof 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 @@ -483,15 +484,11 @@ export function mapWritableState<
G extends _GettersTree<S>,
A,
Keys extends keyof S,
SS extends StateTree = Store<Id, S, G, A>,
>(
useStore: StoreDefinition<Id, S, G, A>,
keys: readonly Keys[]
): {
[K in Keys]: {
get: () => S[K]
set: (value: S[K]) => any
}
}
): Pick<_MapWritableStateReturn<SS>, 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
20 changes: 17 additions & 3 deletions packages/pinia/test-dts/mapHelpers.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ expectType<{
}
}>(mapWritableState(useOptionsStore, ['a']))
// @ts-expect-error: only defined in array
mapWritableState(useStore, ['a']).b
mapWritableState(useOptionsStore, ['a']).b

expectType<{
newA: {
Expand All @@ -126,9 +126,23 @@ expectType<{
}>(mapWritableState(useOptionsStore, { newA: 'a' }))

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

Expand Down

0 comments on commit fe6f21b

Please sign in to comment.