interface Database < T , K > { get ( id ) : T ; set ( id , value ) : void ; } interface Persistable { saveToString ( ) : string ; restoreFromString ( storedState ) : void ; } type DBKeyType = string | number | symbol ; class InMemoryDatabase < T , K extends DBKeyType > implements Database < T , K > { protected db : Record < K , T > = { } as Record < K , T > ; get ( id ) { return this . db [ id ] ; } set ( id , value ) { this . db [ id ] = value ; } } class PersistentMemoryDB < T , K extends DBKeyType > extends InMemoryDatabase < T , K > implements Persistable { saveToString ( ) { return JSON . stringify ( this . db ) ; } restoreFromString ( storedState ) { this . db = JSON . parse ( storedState ) ; } } const myDB = new PersistentMemoryDB < number , string > ( ) ; myDB . set ( "foo" , 22 ) ; console . log ( myDB . get ( "foo" ) ) ; const saved = myDB . saveToString ( ) ; myDB . set ( "foo" , 23 ) ; console . log ( myDB . get ( "foo" ) ) ; const myDB2 = new PersistentMemoryDB < number , string > ( ) ; myDB2 . restoreFromString ( saved ) ; console . log ( myDB2 . get ( "foo" ) ) ;