Applying the Parse-Don't-Validate Principle in TypeScript: Making Illegal States Unrepresentable
By
Christian Ekrem
Summary
This article explores Alexis King's "Parse, don't validate" principle and applies it to TypeScript development. The author argues that most TypeScript codebases validate data at runtime (e.g., checking `if (user.email)`) rather than parsing it into well-typed, validated structures at system boundaries. The piece examines how TypeScript's type system, while powerful, makes this functional programming principle harder to implement than in languages like Haskell or Rust. The author provides practical strategies for structuring TypeScript code to parse external data into precise types that make illegal states unrepresentable, reducing runtime checks scattered throughout the codebase.
Source
Key quotes
· 3 pulledI've been thinking about Alexis King's Parse, don't validate again. I do this quite regularly, actually, usually after staring at a TypeScript codebase that's been quietly accumulating if (user.email) checks like barnacles.
And yet most TypeScript I read — including, embarrassingly, plenty I've written — still validates instead of parsing.
The type system fights back just enough to be annoying.
You might also wanna read
Vercel launches konsistent: an open-source CLI linter for enforcing TypeScript code conventions across humans and AI agents
konsistent is an open-source CLI linter from Vercel that enforces structural conventions in TypeScript codebases. It helps both human develo
Building Faster Parsers Through Data-Oriented Design: Flat Arrays Over Pointer Trees
This article presents a data-oriented design approach to building high-performance parsers, using the author's experience creating Yuku — a
Beyond Call Boundaries: The Case for Semantic Language Interoperability
This article argues that true programming language interoperability goes far beyond simple call boundaries (e.g., "Can JavaScript call Rust?

Comments
Sign in to join the conversation.
No comments yet. Be the first.