

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
The following is used way more often than most of us would like, but it is incredibly useful. Optional properties can be added to an object type with the ? operator:
type Superhero = {
name: string;
strength: number;
cape?: boolean; // cape is optional
};
That means that the type of .cape is actually boolean | undefined, just like optional function parameters.
Do not go overboard with optional props... require all the fields that should be there! It will make your life easier with far fewer runtime checks that look like this:
function fight(superhero: Superhero) {
if (!superhero.cape) {
// contact edna mode
}
// do the happy path thing
}
After being copied on all emails and being overwhelmed, Support.ai management doesn't want to be copied on all mail all of the time, only some mail sometimes...