Capítulo 16 de 36

Chapter 16: Understanding Errors

Core Idea

Because TypeScript's type system is structural, error messages often read as a chain of "why?" sub-messages drilling down from a top-level "not assignable" into the exact incompatible nested property — learning to read that chain top-down turns a wall of text into a precise diagnosis.

Key Concepts

  • "Assignable to": TypeScript's core compatibility relationship — T is assignable to S if a value of type T is an acceptable substitute wherever S is expected (e.g. Cat is assignable to Animal). It governs not just x = y assignment but also function argument passing, return values, and most other places two types meet.
  • The relationship is directional: S assignable to T does not imply T is assignable to S — a subtype flows into a supertype position, not the reverse.
  • Error elaboration chains: a top-level "Type X is not assignable to type Y" is frequently followed by nested sub-messages, each answering "why?" about the one above it — e.g. an object assignment failure elaborates down through "the m property is incompatible" → "because the array element types don't match," ending at the actual root-cause primitive mismatch, even though the top-level types involved looked compatible at a glance.
  • Excess property errors surface as a distinct, blunter message (see Object Types — Excess Property Checks) when an object literal has a field the target type doesn't declare.
  • Union assignment errors point at which member of a union a value fails to match, since a value must satisfy at least one union member to be assignable to the union as a whole.

Key Takeaways

  1. Read a multi-line TypeScript error top-down as a nested chain of "why" — the deepest sub-message is almost always the actual root cause, not the top line.
  2. Remember assignability is directional: don't assume "A is assignable to B" implies you can freely go the other way.
  3. When an error names a nested property (m in the enclosing object), the incompatibility is at that property's type, not the outer object shape itself — check the inner mismatch first.

Connects To

  • Object Types: excess property checking, one of the specific error categories referenced here.
  • Everyday Types / Narrowing: union types, whose assignment failures produce the "no member of this union matches" error shape.