Index Signatures and Record

An index signature describes objects whose keys follow a pattern and whose values share a type. Record<K, V> is a utility type that maps a key type K to a value type V. Use finite key unions with Record when every known key should be present; use index signatures for dictionary-like structures where arbitrary keys are allowed.

Be cautious with dictionary access: a key may not exist at runtime. noUncheckedIndexedAccess can make indexed reads include undefined, encouraging safer handling. Avoid mixing arbitrary values into a typed object unless the domain genuinely supports them.

Typed dictionary
type Feature = "search" | "billing" | "reports";
const enabled: Record<Feature, boolean> = {
  search: true,
  billing: false,
  reports: true
};

const labels: Record<string, string> = {
  ready: "Ready",
  failed: "Failed"
};
💡 The finite Feature union requires all three keys. The string-keyed record allows dictionary-style entries, but reading a missing key may still need runtime handling.
Key Takeaways
  • Use Record for mapped key/value objects.
  • Finite key unions can enforce complete dictionaries.
  • Consider noUncheckedIndexedAccess for safer indexed reads.
Knowledge Check

What does `Record<"a" | "b", number>` require?