Menu

Structural Typing, Excess Property Checks, and satisfies

Lesson 4: Structural Typing, Excess Property Checks, and satisfies

How does TypeScript compare objects?

TypeScript uses structural typing: a value matches a type if it has the required properties with the right types, regardless of how it was created or named. This is sometimes called "duck typing".

Rule Example Result
Extra properties on a variable Passing an object with more fields Allowed
Extra properties on a fresh object literal { name, price, colour } where colour is not in the type Error (excess property check)
Missing required property Object without price Error

Example

interface Product {
  name: string;
  price: number;
}

function showPrice(p: Product) {
  console.log(`${p.name}: ₹${p.price / 100}`);
}

const fromApi = { name: 'Milk 1L', price: 6400, sku: 'MILK-1L' };
showPrice(fromApi); // OK: extra 'sku' is fine on a variable

showPrice({ name: 'Eggs', price: 8400, prise: 8400 });
// Error: Object literal may only specify known properties, and 'prise' does not exist in type 'Product'.

**satisfies** checks that a value matches a type but keeps the value's more specific inferred type:

type SlotConfig = Record<string, { start: string; end: string }>;

const slots = {
  morning: { start: '07:00', end: '11:00' },
  evening: { start: '17:00', end: '21:00' },
} satisfies SlotConfig;

slots.morning.start; // OK: TypeScript knows 'morning' exists
slots.night;         // Error: property 'night' does not exist

How it works

  • Excess property checks apply only to fresh object literals, where extra properties are almost always typos.
  • With an annotation (const slots: SlotConfig = ...), the type widens to Record<string, ...>, so slots.night would be allowed and typed as existing.
  • With satisfies, you get both: validation against SlotConfig and exact keys for autocomplete.
Approach Checks the shape? Keeps exact keys and literals?
const x: T = value Yes No (widened to T)
const x = value satisfies T Yes Yes
const x = value as T No (trusts you) No

Common mistakes

Mistake Effect Fix
Using as Product to silence errors Missing properties go unnoticed Use an annotation or satisfies
Assuming a type name matters for compatibility Two differently named types with the same shape are interchangeable Use branded types if you need them distinct (Module 8)
Annotating config objects and losing literal keys Weaker autocomplete Use satisfies

Key takeaways

  • TypeScript compares shapes, not names.
  • Object literals get extra checks to catch typos.
  • Use satisfies to validate a value while keeping its precise type.

Try it: Create a categoryColours object that satisfies Record<'fruits' | 'dairy' | 'grains', string>, then try removing one key and adding a misspelt one.


TypeScript Tutorial

J

Jebasta





Get in Touch For Details