

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
Most of the time, when you pass an object to a function in TypeScript, it's:
However, when you pass an object literal directly to a function, TypeScript performs what's called "excess property checking". Which means it also will not allow extra properties.
For example, say we have this type:
type Spaceship = {
name: string;
speed: number;
};
and we make an object with one extra property:
const falcon = {
name: "Millennium Falcon",
speed: 75,
weapons: 4,
};
We can pass this object to a function that expects a Spaceship:
function pilot(ship: Spaceship) {
console.log(`Piloting ${ship.name} at ${ship.speed} light-years per hour`);
}
// this is fine
pilot(falcon);
But interestingly, if we pass in the same object literal (no variable assignment), TypeScript will throw an error:
// Error: Object literal may only specify known properties, and 'weapons' does not exist in type 'Spaceship'.
pilot({ name: "Millennium Falcon", speed: 75, weapons: 4 });
It's also worth noting that many of these kinds of rules are configurable in the tsconfig.json file, which we'll cover later. We'll mostly refer to default behavior in this course.
Support.ai management asked to be copied on all emails...