@@ -78,14 +78,11 @@ export class NgtRxStore<TState extends object = any, TRxState extends object = T
7878 super ( ) ;
7979 // set a dummy property so that initial this.get() won't return undefined
8080 this . set ( { __ngt_dummy__ : '__ngt_dummy__' } as TRxState ) ;
81- // call initialize that might be setup by derived Stores
82- this . initialize ( ) ;
8381 // override set so our consumers don't have to handle undefined for state that already have default values
8482 const originalSet = this . set . bind ( this ) ;
8583 Object . defineProperty ( this , 'set' , {
8684 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' ] > ) => {
8986 const firstArg = args [ 0 ] ;
9087 if ( is . obj ( firstArg ) ) {
9188 const modArgs = Object . entries ( firstArg ) . reduce ( ( modded , [ key , value ] ) => {
@@ -94,11 +91,24 @@ export class NgtRxStore<TState extends object = any, TRxState extends object = T
9491 } , { } as NgtAnyRecord ) ;
9592 return originalSet ( modArgs as Partial < TRxState > ) ;
9693 }
97- // @ts -expect-error not sure why ...args here doesn't pass tuple check
9894 return originalSet ( ...args ) ;
9995 } ;
10096 } ,
10197 } ) ;
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 ( ) ;
102112 }
103113
104114 protected initialize ( ) {
0 commit comments