

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Object Literal Types
incomplete
2: Extra Properties
incomplete
3: Optional Object Properties
incomplete
4: Empty Object Type
incomplete
5: Discriminated Unions
incomplete
6: Sets
incomplete
7: Maps
incomplete
8: Dynamic Keys
incomplete
9: Dynamic Default Properties
incomplete
10: PropertyKey
incomplete
11: Readonly Modifier
incomplete
12: 'As Const' and Object.freeze
incomplete
13: Satisfies
incomplete
14: Function Overloads
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Sometimes, you won't know all of an object's property names in advance. For example, say you're building a customer management system where employees can add custom key/value pairs to customer records:
- favoriteColor: "blue"
- favoriteFood: "pizza"
- favoriteAnimal: "cat"
- etc
You can't know what the user will add ahead of time, but you still want to model the data in your program.
You can define dynamic keys using an index signature:
type UserMetrics = {
[key: string]: number;
};
This type says "this object can have any number of properties if the keys are strings and the values are numbers."
const metrics: UserMetrics = {
wordsPerMinute: 50,
errors: 2,
timeOnPage: 120,
};
metrics["refreshRate"] = 60; // OK
metrics["theme"] = "dark"; // Error: Type 'string' is not assignable to type 'number'
Support.ai's email service gets new features often. Let's make it easier to add new settings in the future.
Fix the MailPreferences type. The setPreference function uses it to store a mapping of key/value pairs.