New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Built-in types for JSON? #863
Comments
@aleclarson Hi! Hey, sorry for the delay in reply (have been a bit under the weather this week)
So, thanks for the suggestion. Unfortunately, I don't think I can take on this type on as native. But can maybe offer some help to try and simplify the representation. The following is how I'd represent this type in TypeScript (with the intent to only allow Json encodable values) type JsonObject = { [key: string]: JsonValue }
type JsonArray = JsonValue[]
type JsonValue = (
JsonObject |
JsonArray |
string |
number |
boolean |
null
) The following is the TB representation (which is close to what you have), but where the static types are derived via instantiation expression (ReturnType + Generic Parameter) const JsonObject = <T extends TSchema>(schema: T) => Type.Record(Type.String(), schema)
const JsonArray = <T extends TSchema>(schema: T) => Type.Array(schema)
const JsonValue = Type.Recursive(This => Type.Union([
JsonObject(This),
JsonArray(This),
Type.String(),
Type.Number(),
Type.Boolean(),
Type.Null()
]))
// static types derived from instantiation expression (passing JsonValue)
type JsonObject = Static<ReturnType<typeof JsonObject<typeof JsonValue>>>
type JsonArray = Static<ReturnType<typeof JsonArray<typeof JsonValue>>>
type JsonString = string
type JsonNumber = number
type JsonBoolean = boolean
type JsonNull = null Alternatively, this type can be represented as the following (which is a bit more loose but works) type JsonObject = { [key: string]: JsonValue }
type JsonArray = JsonValue[]
type JsonValue = (
JsonObject |
JsonArray |
string |
number |
boolean |
null
)
const JsonValue = Type.Unsafe<JsonValue>(Type.Any()) So, TypeBox mostly tries to provide analogs for types that can be written in TypeScript, but not to implement specific types such as JsonValue (as these should be constructable via the type builder). The goal is mostly to push the capabilities of TB's type builder such that it has 1-1 parity with TypeScript, and where providing specific constructs (such as JsonValue) moves away from this goal (even though the implementation of analogous types can be somewhat cumbersome). Making representing types of this nature easier from a library design standpoint, that is very much in scope! :) I have been keeping a side project below to explore analogous mappings from TS to TB if you're interested (which is a work in progress) https://sinclairzx81.github.io/typebox-workbench Will close off this issue for now, but let me know if the above works for your use case. I am currently carrying out preliminary design work for the next significant iteration of the TB type builder (which should go out on 0.33.x or 0.34.x), so during these phases, I've very much open to discussing how the builder can be improved and factor user feedback into newer builder / inference designs. Thanks again |
Would you consider adding types for JSON values? With "native" support, we could have better error messages (I think? because Union types don't have great errors last I checked), and I wouldn't have to maintain the nightmare below (馃槅).
My Implementation
The text was updated successfully, but these errors were encountered: