LLearnCode
Academy/typescript/Contracts that survive contact with data

intermediate · 21 minute study

Compatible shapes, excess properties and shallow readonly

Knowing what structural typing does and does not guarantee prevents accidental mutation and misplaced confidence in 'exact' objects.

Loading progressEvidence grows with practice
Useful preparation · 1 prerequisite

Use this as a map, not a lock. If these ideas are familiar, continue.

First, build a mental model

A structural contract lists what a value must provide. It usually does not promise that the value has no other fields, and readonly restricts a typed access path rather than freezing an object.

  1. A value with required compatible properties can satisfy an interface.
  2. Fresh object literals receive extra excess-property checks that catch likely typos.
  3. Intersections require all contracts at once; conflicting properties can become never.
  4. Readonly is shallow unless nested contracts are also readonly. It does not freeze runtime objects.
Read the codetypescript · educational example
interface Player { id: string; score: number }
const stored = { id: 'p1', score: 10, debug: true };
const player: Player = stored;
const profile: Readonly<{ stats: { score: number } }> = { stats: { score: 10 } };
profile.stats.score = 20;
// profile.stats = { score: 30 }; // blocked through this access path
type Impossible = { id: string } & { id: number };
  1. Structural match

    stored has the Player fields, even though it also has debug.

  2. Readonly

    Replacing stats is restricted; modifying its mutable nested score is not.

  3. Intersection

    A shared id would need to be both string and number, producing never.

Production thinking

Take this beyond the example.

Use explicit public projections, immutable update discipline and validation. Treat structural typing as a compatibility tool, not access control.

Sources and further reading

Your evidence

unseen is a local learning signal, not a credential or proof of production competence.

Explain the mental model, then explicitly acknowledge it. Opening a page is not evidence.

Supported retries can complete this lesson. Independent evidence and delayed review determine the skill stage.

Next lesson · Generics preserve relationships →