2 ms·
I've used a few similar libraries [0][1][2] and wrote one for a personal project. If we categorize them as embedded DSLs for runtime type checking with some sup
by AriaMinaei 7y ago
I've used a few similar libraries [0][1][2] and wrote one for a personal project. If we categorize them as embedded DSLs for runtime type checking with some support for static interop, they all share three major flaws:
1. Sub-optimal developer experience: significantly noisier syntax compared to pure typescript, convoluted typescript errors, and slower type checking.
2. Unfixable edge cases in static type checking: Features like conditional types work less reliably on the types produced by the library.
3. Some typescript features can't be supported in the library (again, conditional types).
I think a better way to approach the runtime+static type checking is to do it as a babel plugin, which would fix the DX and edge-case problems and also gracefully degrade in the case of typescript features that can't work in runtime.
Since babel can now parse typescript, it is trivial to write a babel plugin that takes regular typescript files and converts their type annotations into runtime values [4]. Those values can then be fed into a simpler type checking library, giving us runtime type checking on top of the native static type checking experience.
[0] https://github.com/gcanti/io-ts https://github.com/gcanti/io-ts
[1] https://github.com/pelotom/runtypes https://github.com/pelotom/runtypes
[2] mobx-state-tree also has a schema validation library that works to some extent statically: https://github.com/mobxjs/mobx-state-tree/ https://github.com/mobxjs/mobx-state-tree/
[3] https://github.com/gcanti/io-ts#branded-types--refinements https://github.com/gcanti/io-ts#branded-types--refinements
[4] https://gist.github.com/AriaMinaei/2f1229178abad4363f5180db238dd8b3 https://gist.github.com/AriaMinaei/2f1229178abad4363f5180db2...