Skip to content

Instantly share code, notes, and snippets.

@widoz
Created April 20, 2026 21:27
Show Gist options
  • Select an option

  • Save widoz/00ee742483e13fe78f4972e25e90c33a to your computer and use it in GitHub Desktop.

Select an option

Save widoz/00ee742483e13fe78f4972e25e90c33a to your computer and use it in GitHub Desktop.
Immutable Map Wrapper
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