Hacker News new | ask | show | jobs
by sadlion 3353 days ago
How does one validate typescript models at runtime? I have seen combinations of JSON schema and validator libraries but I'd love to do it using typescript's type system. From my understanding, it doesn't work because of type erasure.
3 comments

This is a TypeScript compatible runtime type system for IO validation https://github.com/gcanti/io-ts
You don't. You'd have to use something like https://github.com/hapijs/joi
AFAIK there's no good solution. That's why it works best when your back end is using Typescript as well -- your route returns the same interface the client is expecting.
This goes out the window when you have multiple deployed/running versions of your client or server.
Would one solve this by versioning the types somehow? Honest question; I'm pretty new to TS.