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.
Useful preparation · 1 prerequisite
Use this as a map, not a lock. If these ideas are familiar, continue.
- Follow the compiler through a branch — Narrowing lets one function safely handle success, pending and failure without optional-property guesswork.
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.
- A value with required compatible properties can satisfy an interface.
- Fresh object literals receive extra excess-property checks that catch likely typos.
- Intersections require all contracts at once; conflicting properties can become never.
- Readonly is shallow unless nested contracts are also readonly. It does not freeze runtime objects.
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 };- Structural match
stored has the Player fields, even though it also has debug.
- Readonly
Replacing stats is restricted; modifying its mutable nested score is not.
- 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 →