Why Excess Property Checks Exist
You'll learn the actual purpose behind TypeScript's excess property checking: catching typos and accidental extra data at assignment time.
The Problem TypeScript Solves
Excess property checks exist to catch common mistakes before they cause runtime bugs. Without these checks, you could easily misspell property names or pass objects with unintended extra data, and TypeScript would silently accept them.
Think of it like a spell-checker for your object shapes. If you're filling out a form with specific fields, you want someone to flag when you've written "addres" instead of "address" or when you've accidentally included a field that doesn't belong.
Catching Typos
The most frequent benefit is catching typos in property names:
type User = {
name: string;
email: string;
};
// TypeScript catches the typo immediately
const user: User = {
name: "Alice",
emial: "alice@example.com" // Error: 'emial' doesn't exist
};
Without excess property checks, TypeScript would see emial as an extra property and allow it (since structural typing only cares if required properties exist). Your code would compile, but email would be undefined at runtime.
Preventing Accidental Extra Data
Excess property checks also flag when you're unintentionally passing more data than expected:
type Config = {
timeout: number;
};
const config: Config = {
timeout: 5000,
retries: 3 // Error: 'retries' doesn't exist
};
This helps you catch configuration mistakes where you might be passing options that won't actually be used, indicating a misunderstanding of the API.
Key Takeaway: Excess property checks exist specifically to catch typos and accidental extra properties at the point of assignment, protecting you from structural typing's flexibility when it would hide mistakes rather than help.