We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Empty Object Type

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, because everything else is technically an object! 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",
};

Assignment

Support.ai needs better support for formatting the addresses of their messages... because what use is a message if it doesn't go anywhere?