Expands a record type. By default expands the record to accept any string key.
Record types are annoying to work with in typescript when you need to add keys to them. This utility type properly expands the types of a record type after its creation.
For example, it's possible to map array entries to objects by some key like this (see RecordFromArray):
entries.c// no error entries.d// no error entries.e// no error
// you can also just allow any string key (string is the default if we leave out the key type) constentries = (obj.entriesasExpandRecord<typeofobj.entries>)
entries. // a still gets suggested entries.anything// no error
Note that obj.entries remains incorrectly type, so there's also a utility type to cast a property on a class: ExpandClassRecord.
Expands a record type. By default expands the record to accept any string key.
Record types are annoying to work with in typescript when you need to add keys to them. This utility type properly expands the types of a record type after its creation.
For example, it's possible to map array entries to objects by some key like this (see RecordFromArray):
This is really nice because now those keys get autocompleted:
BUT, the moment you try to add keys, typescript will complain and you will, of course, no longer get the correct suggestions:
You could just ignore this, cast the object as any, etc, but this type provides a more accurate solution that better describes what's going on.
Note that
obj.entries
remains incorrectly type, so there's also a utility type to cast a property on a class: ExpandClassRecord.