

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
So there's this (seemingly) weird but useful thing that you'll see in the wild:
type FormData = {
[field: string]: string;
email: string;
password: string;
};
If what you're concerned about is which types are allowed in the object, you might wonder why email and password are even there. After all, you can specify any string key/value pairs in this type, right?
You use this syntax to require certain properties, in this case, email and password. The type above says:
The object must have an
passwordproperty, and it can have any number of additional string properties.
Here's another example:
type FormData = {
[field: string]: string | number | boolean;
email: string;
password: string;
age: number;
};
This type says:
The object must have an
password(string), andage(number) property, but it can have any number of additional string, number, or boolean properties.
I'd strongly advise against overusing this pattern. Only use dynamic keys when you truly need unknown keys. If you have optional keys, just use the ? operator.
Support.ai needs to communicate whether or not someone is available, so they've added the doNotDisturb and outOfOffice properties to MailPreferences... but someone introduced a bug!