Comments (1)
@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)
- email validation doesn't work? HOT 2
- `Value.Cast` casts to wrong schema when called with union of `Refs` HOT 1
- Support for `definitions`? HOT 1
- Cannot reference a recursive type HOT 7
- Cannot use .map() inside Union with Literal HOT 2
- Nested `Type.Intersect` Errors HOT 1
- Incorrect inferred type for the empty object `Type.Object({})`. HOT 3
- `unevaluatedProperties` failed verification when combined with `Type.Intersect` and `Type.Union` HOT 2
- Types for both CJS and ESM cause ambiguity and prevent correct type resolution. HOT 8
- unique id for schemas generated with Type.Object() HOT 6
- Custom paths for custom validation errors HOT 2
- Composite Union type does not work HOT 3
- Type.Recursive does not work with Type.Transform for StaticDecode
- Issue regarding nested objects when using Value.Default HOT 5
- Enum type inferred as never when using Type.Mapped() HOT 2
- Should `IsValueType` guard consider `Date`? HOT 1
- ESNext target HOT 3
- Trying to do codegen from Drizzle schemas HOT 1
- Make `default` and `examples` schema options properties type-safe HOT 2
- Indicate which files don't have "sideEffects" in package.json for improved tree shaking HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typebox.