Q25: How do `Readonly<T>` and `Record<K, T>` utility types work?

Difficulty:EasyTopic:Utility Types
  • Readonly<T>: Sets all properties of T to readonly, preventing property re-assignment.
  • Record<Keys, Type>: Constructs an object type whose property keys are derived from Keys and whose values are of type Type.
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 Record to construct strongly-typed dictionary maps.