TypeScript · Junior · Question 46 of 75

What happens when you pass an object literal with an extra property?

type User = { name: string };
const u: User = { name: 'Ann', age: 30 };

Explanation

The excess property check applies only to fresh object literals - it's protection against typos. If you first put the object into a variable and then assign it, there's no error (structural compatibility). The compiler doesn't remove anything.

Further reading

Problem with a question

TypeScript · Junior · question 46

Test yourself

Take the TypeScript · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz