Q25: How do `Readonly<T>` and `Record<K, T>` utility types work?
Readonly<T>: Sets all properties ofTtoreadonly, preventing property re-assignment.Record<Keys, Type>: Constructs an object type whose property keys are derived fromKeysand whose values are of typeType.
Record Map Definition
type Page = 'home' | 'about' | 'contact';
interface PageInfo { title: string }
const navPages: Record<Page, PageInfo> = {
home: { title: 'Home' },
about: { title: 'About' },
contact: { title: 'Contact' }
};💡 Record enforces that all listed key members exist in the target mapping dictionary object.
Key Takeaways
- Use
Recordto construct strongly-typed dictionary maps.