Typescript strongly typed Map.
Typescript comes with a generic Map<K, V> type which allows you to set a fixed type for the Keys and another for the Values. This is enough for most cases but sometimes you need more granularity as to which tuples are allowed in the map.
npm install @jfromaniello/typedmap
import { TypedMap } from '@jfromaniello/typedmap';
type KeyValue =
['foo' | 'bar', string] |
[number, string] |
['person', Person];
const map = new TypedMap<KeyValue>();
// Valid examples
map.set('foo', 'hello');
map.set(1, 'world');
map.set('person', new Person());
// Invalid examples
// new Map([['person', 123]]);
// map.set('foo', 'test');
// map.get('fiii');
MIT - 2024 José F. Romaniello