AI Hints
Home›TypeScript Tutorial›Utility Types
TypeScript · Lesson 66 of 120

Record

Typed dictionaries.

⏱ 2 min read▶ Video at 50:49🎓 Free
Watch this lesson in the TypeScript courseWatch on YouTube ↗Full TypeScript course

Record builds an object type from a set of keys, and one value type. With a union of literal keys, every key must be present.

Record
From the video: Record
  • Keys and a value type
  • Record
  • Every key present

Opening hours

Day is a union of three days. Record of day to string must have all three. Run it.

app.tstypescript
type Day = "mon" | "sat" | "sun";

const hours: Record<Day, string> = {
  mon: "9-6",
  sat: "9-5",
  sun: "closed",
};

for (const [day, time] of Object.entries(hours)) console.log(day, time);
Output
mon 9-6
sat 9-5
sun closed

A missing key

Here Sunday is missing. Run it. Record makes sure every day is filled in.

app.tstypescript
type Day = "mon" | "sat" | "sun";
const hours: Record<Day, string> = { mon: "9-6", sat: "9-5" };
Output
app.ts line 2: Property 'sun' is missing in type '{ mon: string; sat: string; }' but required in type 'Record<Day, string>'.

Summary

Record maps a set of keys to one value type, and insists on every key. Next, ReturnType and Parameters.

Key takeaways
  • Record<K, V>
  • All keys
  • Typed values
Next up: ReturnType and Parameters — types from functions