Weak types to strong ones
Use types to catch payment mistakes earlier
These examples help an agent choose and check a code change. Some reshape the types and deliberately reject previously accepted inputs. Keep that behavior decision explicit. A passing type check and a checked Lean property establish different things.
Follow eight changes to an example payments service. Distinct customer and payment IDs help a type checker catch swapped arguments. Separate pending and settled states help it catch missing receipts. Runtime input checks are still needed; unchecked casts can bypass these static checks.
Python and TypeScript, side by side at every stage, with the places they cannot follow each other called out. Click any symbol and the panel says what the tool knows about it: its type, whether that was written down or worked out, where it is defined, what calls it.
Generated Running fr over the
code beside it produced every answer in the panel, and
cargo test --test types_data captured them. The stages live in
tests/types_tutorial and a test asserts the progression, so a stage
that stopped knowing more than the one before it would fail the build.
What "unrepresentable" means here, and what it does not
The type checker catches the illustrated invalid combinations in checked code. Python annotations do not validate values automatically. TypeScript removes type-only declarations from generated JavaScript. At the input boundary, the point where outside data enters the application, validate it before constructing the promised value. Casts and unchecked code can bypass those promises.
That boundary is the whole idea, and other people made it: Alexis King's Parse, Don't Validate is the short version, Yaron Minsky's "make illegal states unrepresentable" the older phrase, and Scott Wlaschin's Domain Modeling Made Functional the worked one. The stages below apply those arguments to code that already exists, one refactoring at a time. Fowler's catalogue names most of them under Primitive Obsession.
Inspect what the source declares
The tool answers a symbol's type from what the source declared, or from what follows from it: a literal, a class constructed, a declared return type. It always says which of the two it is doing. At the first stage almost everything is worked out, and a good deal is not known at all; by the last, almost everything is written down. Watch that number move from stage to stage.
Where the two languages stop agreeing
| Idea | Python 3.14 | TypeScript 7 |
|---|---|---|
| A closed set of names | StrEnum, a real object at run time, with members you can iterate | a union of string literals; enum exists and is best avoided |
| A name of its own for a string | NewType, a distinct checked label; constructor calls still run | a brand using unique symbol, checked during type checking |
| Immutability | @dataclass(frozen=True), enforced when it runs | readonly, a note to the checker and nothing more |
| A tagged union | a union of classes; match narrows by class | a discriminant field is required, and switch narrows on it |
| Proving every case is handled | assert_never | const _: never = x |
| A private constructor | convention: look in Money.of, though nothing sends you there | private constructor, enforced by the checker |
The last row is the sharpest. TypeScript can stop you calling
new Money(-1). Python cannot stop you calling
Money(-1, USD); it only asks that you do not. Both make the invalid value hard to reach by accident, and a
refactoring buys you that much. Neither makes it impossible, and a page that told
you otherwise would be selling something.
What stayed a run-time check, and why
One check survives to the last stage: a refund may not exceed what was captured. It relates two values instead of naming a state, and no type here can say "no larger than the number in that other object". Languages that can say it exist; these two cannot.
The technique stops there. Everything above it, the states and the identifiers and the currencies and the verifications, went from a check somebody had to remember to a shape the checker enforces. This one did not, and pretending otherwise would undo the point of the other seven.