3 ms·
I developed a pattern in typescript (I'm sure it's not original) where I have an interface describing an API entity and a class of the same name with only stati
by Waterluvian 6y ago
I developed a pattern in typescript (I'm sure it's not original) where I have an interface describing an API entity and a class of the same name with only static methods, one of which is Foo.fromApi() that validates and parses.
I haven't seen any need to bring a library in to handle this. Though it would be nice to marry the worlds of TS, API, and Json Schema.
- lhnz 6y agoIt doesn't use json schema but you might be interested in something like this: https://gcanti.github.io/io-ts/ https://gcanti.github.io/io-ts/ (You can define runtime encoder/decoders which produce typed values.)
- brundolf 6y agoio-ts is fantastic (I linked it myself above). The killer feature is that it infers the static types of your runtime schemas for you, so you don't have to define them twice. You make a change to the schema, the rest of your code will typecheck against it.