← Effect fixes your TypeScript

Effect fixes your TypeScript · Part 9

"as User" is not validation

· by Tony Tang

res.json() as User means bad data fails three files later. Effect Schema decodes at the boundary, fails with a typed error pointing at the exact field, and gives you the TypeScript type for free.

Comment below: How many as casts are in your codebase?

Effect fixes your TypeScript: a series on the bugs you already ship, and how Effect makes them impossible. All code is real Effect v4 and was compiled before it went on screen. https://effect.website

Transcript

"as User" is not validation. It's a promise you can't keep. res.json() as User. TypeScript believes you, but the API renamed email to emailAddress last week. Nothing fails here. It fails three files later. Cannot read properties of undefined. Far from the cause in production. With Effect Schema, you describe User once. Decode at the boundary. Bad data fails right here as a typed error that points at the exact field. The TypeScript type comes free from the schema. And it works both ways. Decode a date string into a date, encode it back for the API. Best practice. Validate at the boundary, trust inside it. One schema, one source of truth. There's one input you trust even more blindly than your API. process.env. Part 10. How many "as" casts are in your codebase?

Full lesson in the Learn Effect course ↗ Validation in the error channel

More from this series