The Weird Parts of TypeScript 13 exercises

Excess Properties in Functions

Let's look at another example where TypeScript does not trigger excess property warnings as we might expect.

Here we have a User interface with id and name properties, and a users array containing two user objects, "Waqas" and "Zain".

interface User {
id: number;
name: st

Loading exercise


00:00 Let's look at another example where access property warnings don't trigger. We have an interface of user with ID and name, and then we have some users here, which is an array of just names, so Vacas and Zayin. Now, when we say users with IDs, we're assigning that variable to a type of user.

00:19 And then we say user index, we map the rest of the user, or we spread the rest of the user into this object we're creating, and then we give it an ID, but we're also seemingly able to add an age of 30 here without TypeScript erroring at us.

00:37 So if we remove the TS expect error, doesn't that look weird? Users with IDs, it's not expecting to have an age on there, but at runtime it will have an age on there. So why isn't TypeScript yelling at us? Because it seems like we've added a nice little annotation. If we remove the annotation, sure, then users with IDs,

00:55 it's going to be an array with ID, age, and name on it. But if we add the annotation, there's no error. Weird. See if you can work out why this might be happening and what we can do to make it error in the correct way. Good luck.