

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 5
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
Say I innocently create a new empty object:
let newUser = {};Then go to add properties to it later:
// Property 'name' does not exist on type '{}'
newUser.name = "Lane";TypeScript doesn't like that!
It makes sense, we never told TypeScript which properties to allow... but here's what's really crazy: this is actually allowed:
let newUser = {};
newUser = "Lane";Yup. You can reassign the variable, which initially held an empty object to a string. In fact, you can reassign it to anything except null or undefined. So, to get back to our first example, what you probably want to do is just predefine the allowed field(s):
type User = {
name: string;
};
let newUser: User = {
name: "Lane",
};Support.ai needs better support for formatting the addresses of their messages... because what use is a message if it doesn't go anywhere?