Open allonzy opened 3 years ago
I am not very optimistic about the possibility of it, but please do try - it would be interesting.
JSON Schema, is general, is not aligned with TS type system - it allows expressing constraints that are not expressible in typescript type system. Also, in some cases, I think there might be more than one possible type derived from JSON Schema (can be wrong here).
Neither is a complete blocker - for the first problem, you would have to define a subset of JSON Schema that is mappable to TS types, and have this type fail on schemas outside of this subset - that actually could be useful. For the second, we will just have to make some choices...
The type name should be JSONSchemaDataType
cc @erikbrinkman
Also, in some cases, I think there might be more than one possible type derived from JSON Schema
Well, that might be problematic, as you say choice has to be made in that case
But the other way is not, i know that jsonSchema allow complex constraints that typescript, and that's a huge part of why i'm using it, but in a developer perspective having the static type hinting and compilation error, even on a superset type is far better than having nothing at all
Starting the implementation when i can
I generally agree with @epoberezkin on all things here. I'm also not sure if there could be multiple possible types, but this exists for JTDDataType too with timestamps being interpreted as Date | string
, so it's not the end of the world, even if it's not great.
My guess is you should be able to start with JTDDataType as a backbone but swap in JSON Schema structures. It seems like it could work at least for simple schemas.
There are a few risks I want to call out.
I hope this proves fruitful!
What version of Ajv you are you using? 8.6.4 What problem do you want to solve? I want to dynamically infer ts types from a json schema, it will reduce the need for auto-generated file or code-duplication (define your interface 2 times, one for ts, one for ajv) What do you think is the correct solution to problem? Implement a type mapper like in JTDDataType from 'ajv/dist/jtd', and update readme/doc accordingly Will you be able to implement it? Yes