What does HackerNews think of zod?
TypeScript-first schema validation with static type inference
Optional chaining or null checks should not be used "defensively". They should just be used whenever TypeScript tells you to. Otherwise you just exacerbate the problem and end up with more unanticipated null values.
If you're dealing with an API with untrustworthy types or lots of null values, the solution is to:
a) Write mostly optional type definitions for that API, and/or
b) Use zod to verify the API data before you use it, to make sure it matches your expectations.
As far as I can tell, my go-to schema system doesn’t. https://github.com/colinhacks/zod
I strongly recommend Zod[0]. You write your schemas in code, it validates incoming JSON at runtime, and you end up with either an error or well typed code. The schemas themselves are powerful (you can check properties of strings, etc), and the derived typescript type comes out for free and looks like a human wrote it. A very powerful tool, with a very intuitive interface.
Give elixir a serious go and see if you ever say this again.
Ah you hit me where it hurts - really need to seriously try the Erlang ecosystem.
Recently, I've been using Zod [2] and find it to be a satisfying equivalent: you define a schema, and then you get both a TS type AND a JS parser/validator (which works as a TS typeguard).
[1] https://github.com/microsoft/TypeScript/wiki/TypeScript-Desi... [2] https://github.com/colinhacks/zod
It’s worth noting that GitHub stars truly don’t translate into anything worthwhile in the vast majority of cases. You can have a massively popular project, but it doesn’t automatically translate into Twitter followers or newsletter subscribers. And those things don’t directly translate into wealth or happiness either.
Just get a job and make friends and have a life. That route has the highest expected value. I have been chasing GitHub stars for a long time, and it has never been fulfilling. I wouldn’t recommend it.
The idea was that API responses that weren't reflected in the types would cause hard errors that we had to fix, and that we'd be forced to make sure that the mocks in our tests were up-to-date valid responses. Similarly to the article, I also made it so that in production they would log warnings, because I didn't want to expose us to the possibility that validation could take down the app.
However, TypeScript is new to my current team, and so nobody wanted the introduction of a validation API on top. It ended up being ~20 lines of code I couldn't get merged. Since TypeScript was new to the team syntax like `z.infer`, generic code and a whole new way to define types at runtime made them feel like it was too complicated.
Did others have similar problems introducing something like this?
I really would like to see nominal typing support in TypeScript. Currently, it's hard to validate a piece of data (or parse for that matter) once and have other functions only operate on that validated data. There are (ugly?) workarounds though [1].
[0]: https://github.com/colinhacks/zod [1]: https://gist.github.com/dcolthorp/aa21cf87d847ae9942106435bf...