type-fest
type-fest copied to clipboard
RecordToMap type
Maps are generally safer than records at runtime, but so-called "mapped types" can only be records. As such, I propose this type:
type RecordToMap<T extends Record<PropertyKey, any>> =
UnionToIntersection<ValueOf<{ [k in keyof T]: Map<k, T[k]> }>>;
This gives stricter typing than Map<keyof T, ValueOf<T>>: rather than Map<'foo' | 'bar', FooType | BarType>, this gives Map<'foo', FooType> & Map<'bar', BarType>.