1
1
import type { ComponentPublicInstance , ComputedRef , UnwrapRef } from 'vue-demi'
2
2
import type {
3
3
_GettersTree ,
4
- _Method ,
4
+ _StoreWithGetters_Writable ,
5
5
StateTree ,
6
6
Store ,
7
7
StoreDefinition ,
@@ -431,23 +431,35 @@ export function mapActions<
431
431
/**
432
432
* For internal use **only**
433
433
*/
434
- export type _MapWritableStateReturn < S > = {
435
- [ key in keyof S ] : {
436
- get : ( ) => S [ key ]
437
- set : ( value : S [ key ] ) => any
434
+ export type _MapWritableStateKeys < S extends StateTree , G > =
435
+ | keyof UnwrapRef < S >
436
+ | keyof _StoreWithGetters_Writable < G >
437
+
438
+ /**
439
+ * For internal use **only**
440
+ */
441
+ export type _MapWritableStateReturn <
442
+ S extends StateTree ,
443
+ G ,
444
+ Keys extends _MapWritableStateKeys < S , G > ,
445
+ > = {
446
+ [ key in Keys ] : {
447
+ get : ( ) => UnwrapRef < ( S & G ) [ key ] >
448
+ set : ( value : UnwrapRef < ( S & G ) [ key ] > ) => any
438
449
}
439
450
}
440
451
441
452
/**
442
453
* For internal use **only**
443
454
*/
444
455
export type _MapWritableStateObjectReturn <
445
- S ,
446
- T extends Record < string , keyof S > ,
456
+ S extends StateTree ,
457
+ G ,
458
+ KeyMapper extends Record < string , _MapWritableStateKeys < S , G > > ,
447
459
> = {
448
- [ key in keyof T ] : {
449
- get : ( ) => S [ T [ key ] ]
450
- set : ( value : S [ T [ key ] ] ) => any
460
+ [ key in keyof KeyMapper ] : {
461
+ get : ( ) => UnwrapRef < ( S & G ) [ KeyMapper [ key ] ] >
462
+ set : ( value : UnwrapRef < ( S & G ) [ KeyMapper [ key ] ] > ) => any
451
463
}
452
464
}
453
465
@@ -462,13 +474,13 @@ export type _MapWritableStateObjectReturn<
462
474
export function mapWritableState <
463
475
Id extends string ,
464
476
S extends StateTree ,
465
- G extends _GettersTree < S > ,
477
+ G ,
466
478
A ,
467
- KeyMapper extends Record < string , keyof UnwrapRef < S > > ,
479
+ KeyMapper extends Record < string , _MapWritableStateKeys < S , G > > ,
468
480
> (
469
481
useStore : StoreDefinition < Id , S , G , A > ,
470
482
keyMapper : KeyMapper
471
- ) : _MapWritableStateObjectReturn < UnwrapRef < S > , KeyMapper >
483
+ ) : _MapWritableStateObjectReturn < S , G , KeyMapper >
472
484
/**
473
485
* Allows using state and getters from one store without using the composition
474
486
* API (`setup()`) by generating an object to be spread in the `computed` field
@@ -480,13 +492,13 @@ export function mapWritableState<
480
492
export function mapWritableState <
481
493
Id extends string ,
482
494
S extends StateTree ,
483
- G extends _GettersTree < S > ,
495
+ G ,
484
496
A ,
485
- Keys extends keyof UnwrapRef < S > ,
497
+ Keys extends _MapWritableStateKeys < S , G > ,
486
498
> (
487
499
useStore : StoreDefinition < Id , S , G , A > ,
488
500
keys : readonly Keys [ ]
489
- ) : Pick < _MapWritableStateReturn < UnwrapRef < S > > , Keys >
501
+ ) : Pick < _MapWritableStateReturn < S , G , Keys > , Keys >
490
502
/**
491
503
* Allows using state and getters from one store without using the composition
492
504
* API (`setup()`) by generating an object to be spread in the `computed` field
@@ -498,43 +510,51 @@ export function mapWritableState<
498
510
export function mapWritableState <
499
511
Id extends string ,
500
512
S extends StateTree ,
501
- G extends _GettersTree < S > ,
513
+ G ,
502
514
A ,
503
- KeyMapper extends Record < string , keyof S > ,
515
+ Keys extends _MapWritableStateKeys < S , G > ,
516
+ KeyArr extends Keys [ ] ,
517
+ KeyMapper extends Record < string , Keys > ,
504
518
> (
505
519
useStore : StoreDefinition < Id , S , G , A > ,
506
- keysOrMapper : Array < keyof S > | KeyMapper
507
- ) : _MapWritableStateReturn < S > | _MapWritableStateObjectReturn < S , KeyMapper > {
520
+ keysOrMapper : KeyArr | KeyMapper
521
+ ) :
522
+ | _MapWritableStateReturn < S , G , Keys >
523
+ | _MapWritableStateObjectReturn < S , G , KeyMapper > {
508
524
return Array . isArray ( keysOrMapper )
509
- ? keysOrMapper . reduce ( ( reduced , key ) => {
510
- // @ts -ignore
511
- reduced [ key ] = {
512
- get ( this : ComponentPublicInstance ) {
513
- // @ts -expect-error: FIXME: should work?
514
- return useStore ( this . $pinia ) [ key ]
515
- } ,
516
- set ( this : ComponentPublicInstance , value ) {
517
- // @ts -expect-error: FIXME: should work?
518
- return ( useStore ( this . $pinia ) [ key ] = value )
519
- } ,
520
- }
521
- return reduced
522
- } , { } as _MapWritableStateReturn < S > )
525
+ ? keysOrMapper . reduce (
526
+ ( reduced , key ) => {
527
+ reduced [ key ] = {
528
+ get ( this : ComponentPublicInstance ) {
529
+ return useStore ( this . $pinia ) [ key ] as ( S & G ) [ typeof key ]
530
+ } ,
531
+ set (
532
+ this : ComponentPublicInstance ,
533
+ value : Store < Id , S , G , A > [ typeof key ]
534
+ ) {
535
+ return ( useStore ( this . $pinia ) [ key ] = value )
536
+ } ,
537
+ }
538
+ return reduced
539
+ } ,
540
+ { } as _MapWritableStateReturn < S , G , Keys >
541
+ )
523
542
: Object . keys ( keysOrMapper ) . reduce (
524
543
( reduced , key : keyof KeyMapper ) => {
525
- // @ts -ignore
526
544
reduced [ key ] = {
527
545
get ( this : ComponentPublicInstance ) {
528
- // @ts -expect-error: FIXME: should work?
529
- return useStore ( this . $pinia ) [ keysOrMapper [ key ] ]
546
+ return useStore ( this . $pinia ) [ keysOrMapper [ key ] ] as ( S &
547
+ G ) [ KeyMapper [ typeof key ] ]
530
548
} ,
531
- set ( this : ComponentPublicInstance , value ) {
532
- // @ts -expect-error: FIXME: should work?
549
+ set (
550
+ this : ComponentPublicInstance ,
551
+ value : Store < Id , S , G , A > [ KeyMapper [ typeof key ] ]
552
+ ) {
533
553
return ( useStore ( this . $pinia ) [ keysOrMapper [ key ] ] = value )
534
554
} ,
535
555
}
536
556
return reduced
537
557
} ,
538
- { } as _MapWritableStateObjectReturn < S , KeyMapper >
558
+ { } as _MapWritableStateObjectReturn < S , G , KeyMapper >
539
559
)
540
560
}
0 commit comments