3 ms·
This is great. I recently built a fairly large amount of functionality around io-ts, adding support for mixed required/optional fields, eliminating the need to
by eyelidlessness 7y ago
This is great. I recently built a fairly large amount of functionality around io-ts, adding support for mixed required/optional fields, eliminating the need to deal with `Either` monads, and a whole lot of other stuff that's mostly just relevant to my company's usage. I think if this had been available at the time I was looking for an underlying library, it would have been a no brainer to choose this one.
Worth noting, another option is Runtypes[1], which also looks great. I can't remember off the top of my head why I ultimately picked io-ts over Runtypes, but it's another one for folks to consider (and I'd be curious what the Zod author thinks of it).
https://github.com/pelotom/runtypes https://github.com/pelotom/runtypes
- eyelidlessness 7y agoOh, I do want to add one bit of minor feedback for the author: one of the things I like about the io-ts interface is that fields/codecs are values unless they require a parameter (e.g. `t.string` vs `z.string()`). It's a little bit easier for me to read. It's also not clear to me at a glance whether calling `z.string()` is creating a new instance of something and whether that affects the behavior of a given schema.
- colinmcd 7y agoI personally prefer to standardize everything as a function. It also leaves some breathing room in case I decide to include parameters as part of a future API augmentation.
- eyelidlessness 7y agoI hope you'll give me the opportunity to try to convince you otherwise. I understand the context of this is that io-ts puts an onerous emphasis on FP concepts and data structures. But FP principles, when applied in an effective and usable way, are quite good. The make it easier to reason about and maintain code. One such principle is that a "function" with no parameters is a good sign that it will cause some kind of side effect. I sort of hinted at this when I questioned the design, when I said that it wasn't clear to me whether calling these functions was producing new instances and whether those new instances affect behavior. I would also argue that leaving room to parameterize primitive types is leaving room to make a drastically more complicated API in the future. A `string` with "options" suddenly becomes its own sub-API. While io-ts is more complicated, once learnt its API is more predictable. A `Type` is a value. An `interface` (or similar) type is a `Record<string, Type>`. Always. `Type`s can be augmented/composed/etc (and you may provide a function that accepts one or more `Type`s as parameters to accomplish that), but they always resolve to a value. The value can be reused without concern about side effects.
- Ezku 7y agoVery strongly agreeing with this assessment and hoping Colin will come to agree. A zero-argument function is either a constant, or a side-effect. Given we don’t want a side-effect, exposing a constant instead of an effectful-looking function is preferable.
- colinmcd 7y agoRe: runtypes. Not sure how I missed this, it looks like an excellent tool. No support for recursive types (which I personally want/need for my project). I really like their API for constraint checking...might have to steal that... [UPDATE] runtypes does support recursive types! My bad!
- btown 7y agoSecond runtypes! It's really Yup but Typescript-first, which is exactly what a lot of people need, per OP's article. The great thing is that there's no need to standardize these libraries - it's trivial, for instance, to build a quick utility wrapper around https://react-hook-form.com/api/#validationResolver https://react-hook-form.com/api/#validationResolver that hooks into your runtime-compiletime schema library of choice!
- eropple 7y agoFWIW, runtypes is now part of my standard utility kit (along with utility-types[0]) for every TypeScript project I start. I don't leave home without it, and (AFAIK) it checks off every box mentioned as a problem by the author of the article. At my employer, I just whacked it straight into our new API codebase on day one and people took to it immediately. It's great, great stuff. [0]: https://github.com/piotrwitek/utility-types https://github.com/piotrwitek/utility-types