← Retour au blog
tech 30 June 2026

Parse, Don't Validate – In a Language That Doesn't Want You To

Discover why validation alone isn't enough and how parsing can enhance the robustness of your code, even in TypeScript.

Article inspired by the original source
Parse, Don't Validate – In a Language That Doesn't Want You To ↗ cekrem.github.io

# Parse, Don't Validate – In a Language That Doesn't Want You To

Data validation is a crucial topic in software development. Yet, in languages like TypeScript, a common approach is to validate data without parsing it. But is that really enough? In reality, parsing data offers increased robustness and better type management, even in a language that doesn't naturally nudge you this way.

The Difference Between Validation and Parsing

Validation simply checks if the data meets certain criteria, while parsing transforms this data into a more precise type. For example, validating an email checks if a string contains an '@', whereas parsing an email transforms the string into an EmailAddress type if it's valid. Once parsed, the information is preserved, eliminating the need to re-check these data further down the code.

Why Parsing is Crucial

In languages like Haskell or Elm, this approach is natural due to their strong typing. However, TypeScript, with its structural typing, doesn't guide you in this direction. This can lead to errors where invalid data slips through the cracks because the type system forgets the validation information as soon as it's done.

Imagine a function isValidUser which checks if a user has a valid email and a reasonable age. Once this validation is done, the type system hasn't retained this information. Thus, other parts of the code must re-check this same data, which is not only inefficient but also a potential source of errors.

Implementing Parsing in TypeScript

To implement parsing in TypeScript, you can use advanced typing techniques. For instance, instead of merely validating a string as an email, you can create an EmailAddress type that encapsulates validation and parsing.

```typescript interface EmailAddress { value: string; }

function parseEmail(email: string): EmailAddress | null { if (email.includes("@")) { return { value: email }; } return null; } ```

With this approach, once an email is parsed, you're sure it's a valid email every time you use it.

Benefits of Parsing

  • Increased Security: Reduce errors by eliminating redundant checks.
  • Code Readability: The code becomes clearer and more expressive as each type carries its own significance.
  • Easier Maintenance: Fewer checks mean less code to maintain.

Going Further

Tools like io-ts and zod can help you define safer types and integrate parsing into TypeScript. These libraries allow you to create validated types declaratively, reducing repetitive code and increasing robustness.

Conclusion

Adopting parsing rather than validation in languages like TypeScript may seem counterintuitive, but the benefits are clear: safer, more maintainable code and avoided errors. So why not start parsing your data today?

Let's discuss your project in 15 minutes.

TypeScript Parsing Validation Type-Safety Functional Programming
Deepthix newsletter · 100% AI · every Monday 8am

An AI agent reads tech for you.

Our AI agent scans ~200 sources per week and ships the best articles to your inbox Monday 8am. Free. One click to unsubscribe.

Visit the newsletter page →

Want to automate your operations?

Let's talk about your project in 15 minutes.

Book a call