3 ms·
Have been using io-ts to accomplish the latter. We create io-ts types first and then leverage its TypeOf function to generate the regular TS types.
by pikamonad 7y ago
Have been using io-ts to accomplish the latter. We create io-ts types first and then leverage its TypeOf function to generate the regular TS types.
- lf-non 7y agoThe biggest advantages of io-ts over the jsonschema based type generation approach is that our data doesn't have to be json and we can handle non-serializable and complex data structures eg. class instances etc. This makes it additionally useful for scenarios where we are writing a typescript library but also want good descriptive error messages for users who use vanilla javascript. Even for type-checking at io boundaries [1], it is more useful than jsonschema because using io-ts we can, besides validating the data, also transform our incoming and outgoing data into richer data-structures (eg. string <-> date instance, plain javascript objects <-> class instances) through use of encoders and decoders. [1] https://lorefnon.tech/2018/03/25/typescript-and-validations-at-runtime-boundaries/ https://lorefnon.tech/2018/03/25/typescript-and-validations-...