Giter VIP home page Giter VIP logo

Comments (1)

sinclairzx81 avatar sinclairzx81 commented on July 4, 2024

@aleclarson Hi! Hey, sorry for the delay in reply (have been a bit under the weather this week)

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 (😆).

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
S

from typebox.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.