type Internal < T > = { $current : T | null ; $reset : ( ) => void ; } ; type Store < T > = T & Internal < T > ; type Initializer < T > = ( ) => T ; type PropertyHandlerName = 'get' | 'set' | 'has' | 'defineProperty' | 'deleteProperty' ; export const createStore = < T extends { } > ( initializerFn ) : Store < T > => { const internal = { $current : null , $reset ( ) { internal . $current = null ; } , } ; const propertyHandler = < Handler extends PropertyHandlerName > ( handler ) => ( target , property , ... args ) => { if ( String ( property ) . startsWith ( '$' ) ) { return ( Reflect [ handler ] as any ) . call ( Reflect , target , property , ... args ) ; } else { if ( ! internal . $current ) { internal . $current = initializerFn ( ) ; } return ( Reflect [ handler ] as any ) . call ( Reflect , internal . $current , property , ... args ) ; } } ; return new Proxy ( internal , { get : propertyHandler ( 'get' ) , set : propertyHandler ( 'set' ) , has : propertyHandler ( 'has' ) , deleteProperty : propertyHandler ( 'deleteProperty' ) , defineProperty : propertyHandler ( 'defineProperty' ) , } ) as Store < T > ; } ;