Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): unwrap refs in mapWritableState for setup stores #2805

Merged
merged 3 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
posva marked this conversation as resolved.
Show resolved Hide resolved

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