type-fest icon indicating copy to clipboard operation
type-fest copied to clipboard

RecordToMap type

Open bbrk24 opened this issue 2 years ago • 0 comments

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>.

bbrk24 avatar Oct 01 '23 21:10 bbrk24