Chapter 1
Contracts and Interfaces
A contract describes what one part of a system promises to another.
Contracts include inputs, outputs, error shapes, side effects, and compatibility rules. An interface can be a TypeScript type, an HTTP schema, a function signature, or a team agreement. Its value is a stable boundary, not the syntax used to declare it.
A useful contract states required and optional fields, validates data at runtime where trust changes, and makes failures explicit. Changes should be additive when possible. Removing or changing meaning needs versioning or a coordinated migration.
For an order API, define currency, amount units, status values, authentication, and error codes before building screens. Frontend, backend, tests, and monitoring can then share the same behavior.
A clear contract reduces coordination cost and makes change safer.
How to apply this
- Validate data at system boundaries.
- Document errors and side effects, not only success fields.
- Plan compatibility before changing an existing contract.
Situation
Backend changes price from cents to decimal currency without changing the schema.
Decision
Create an explicit money contract, version the change, and migrate consumers with contract tests.