Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Maybe a better solution (not necessarily for your exact your use case) would be to generate the Typescript types from JSON schema? The schema feels more like it should be the real source of truth. That's how protobuf (for example) works - there's a language-independent schema and you can generate types for any languages you wish (but don't have to depend on any particular language if you don't need it).


This is pretty doable with mapped types in TS, to make it work without a compilation step though is pending on https://github.com/microsoft/TypeScript/issues/32063

Although in practice the mapped types to do this do slow down the typing service non trivially.


Our original build path was:

1. Fancy combo of build and compile time generics generated all of our libraries 2. Tooling ran over return values from the generic definitions and created the schema

I rewrote it and now everything is defined in JSON files and those JSON files are ran through a code generator that creates our exported libraries, and we still generate the schema based on the TS exports.

Having everything defined in JSON then allowed us to write tooling on top of the JSON to make changes to our libraries.

Protobuf v3 is horrible, had to start using it recently. The type system is so anemic, it is a joke how hard it is to model things in it.

JSON schema is more powerful than TS, and TS is orders of magnitude more powerful than what can be expressed in PB.

The original generics code was super cool, and obscenely succinct, but it wasn't amenable to being auto generated.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: