@@ -78,14 +78,11 @@ export class NgtRxStore<TState extends object = any, TRxState extends object = T
78
78
super ( ) ;
79
79
// set a dummy property so that initial this.get() won't return undefined
80
80
this . set ( { __ngt_dummy__ : '__ngt_dummy__' } as TRxState ) ;
81
- // call initialize that might be setup by derived Stores
82
- this . initialize ( ) ;
83
81
// override set so our consumers don't have to handle undefined for state that already have default values
84
82
const originalSet = this . set . bind ( this ) ;
85
83
Object . defineProperty ( this , 'set' , {
86
84
get : ( ) => {
87
- // Parameters type does not do well with overloads (RxState#set). So we use any[] here
88
- return ( ...args : any [ ] ) => {
85
+ return ( ...args : Parameters < RxState < TRxState > [ 'set' ] > ) => {
89
86
const firstArg = args [ 0 ] ;
90
87
if ( is . obj ( firstArg ) ) {
91
88
const modArgs = Object . entries ( firstArg ) . reduce ( ( modded , [ key , value ] ) => {
@@ -94,11 +91,24 @@ export class NgtRxStore<TState extends object = any, TRxState extends object = T
94
91
} , { } as NgtAnyRecord ) ;
95
92
return originalSet ( modArgs as Partial < TRxState > ) ;
96
93
}
97
- // @ts -expect-error not sure why ...args here doesn't pass tuple check
98
94
return originalSet ( ...args ) ;
99
95
} ;
100
96
} ,
101
97
} ) ;
98
+
99
+ // override get to return {} if get() returns undefined
100
+ const originalGet = this . get . bind ( this ) ;
101
+ Object . defineProperty ( this , 'get' , {
102
+ get : ( ) => {
103
+ return ( ...args : Parameters < RxState < TRxState > [ 'get' ] > ) => {
104
+ const state = originalGet ( ...args ) ;
105
+ return state || { } ;
106
+ } ;
107
+ } ,
108
+ } ) ;
109
+
110
+ // call initialize that might be setup by derived Stores
111
+ this . initialize ( ) ;
102
112
}
103
113
104
114
protected initialize ( ) {
0 commit comments