The Record utility type creates object types with specific key and value types. Here's how to use it effectively.
Basic Record#
Enum Keys#
Complex Value Types#
Mapped Type Comparison#
Partial Record#
Record with Index Signature#
Type-Safe Configuration#
Lookup Tables#
State Machines#
Translation/i18n#
Event Handlers#
API Response Mapping#
Record with keyof#
Best Practices#
When to Use Record:
✓ Dictionary/map structures
✓ Lookup tables
✓ Configuration objects
✓ Known set of keys
Patterns:
✓ Union types as keys
✓ Enum values as keys
✓ keyof for interface keys
✓ Combine with Partial
Type Safety:
✓ All keys required by default
✓ Use Partial for optional keys
✓ Leverage autocomplete
✓ Exhaustiveness checking
Avoid:
✗ Using 'any' as key type
✗ Overcomplicating simple objects
✗ Ignoring undefined values
✗ Mixing with index signatures
Conclusion#
Record is a powerful utility type for creating type-safe dictionaries and mappings. Use it with union types or enums for exhaustive key checking, combine with Partial for optional keys, and leverage it for configuration objects, lookup tables, and state machines. It provides better type inference and autocomplete compared to plain index signatures.