Created
April 20, 2026 21:27
-
-
Save widoz/00ee742483e13fe78f4972e25e90c33a to your computer and use it in GitHub Desktop.
Immutable Map Wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export class ImmutableRecord< T > { | |
| readonly #map: Readonly< Record< string, T > >; | |
| public constructor( map: Readonly< Record< string, T > > = {} ) { | |
| this.#map = map; | |
| } | |
| public get< F >( key: string, fallback?: F ): T | F | undefined { | |
| return this.#map[ key ] ?? fallback; | |
| } | |
| public set( key: string, value: T ): ImmutableRecord< T > { | |
| return new ImmutableRecord( { ...this.#map, [ key ]: value } ); | |
| } | |
| public delete( key: string ): ImmutableRecord< T > { | |
| const { [ key ]: _removed, ...rest } = this.#map; | |
| void _removed; | |
| return new ImmutableRecord( rest ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment