Extra Properties
What
TypeScript has specific rules for how it handles objects that have more properties than those defined in a target type.
How
When you assign an existing object to a variable of a specific type, extra properties are generally allowed. However, when you pass an object literal directly to a function, TypeScript performs excess property checking, which forbids extra properties.
type Spaceship = {
name: string;
speed: number;
};
const falcon = { name: "Falcon", speed: 75, weapons: 4 };
function pilot(ship: Spaceship) { /* ... */ }
pilot(falcon); // OK: Variable assignment allows extra props
pilot({ name: "Falcon", speed: 75, weapons: 4 }); // Error: Direct literal forbids extra props
Why
Excess property checking is designed to catch typos. If you pass a literal with an extra property, it’s often because you misspelled a required property or mistakenly thought a certain field was supported.
Summary
While objects can technically have extra data, direct literals are strictly checked to ensure they match the expected schema exactly.