Closed Victugord closed 1 year ago
@Victugord You can get the type similarly to Zod. I would prefer Zod since my whole code base uses it but, this works.
import { Static } from '@sinclair/typebox'
import { t } from 'elysia'
const Test = t.Undefined()
type Test = Static<typeof Test>
I have been experimenting with TypeBox and Zod back then in the beta day in fact I did ship Zod in some versions before migrating to TypeBox, because I have some problems back then that I don't remember anymore, mainly a performance difference between Zod and TypeBox (see benchmark).
As performance is a critical factor for the server side, and find that TypeBox is a good trade-off for that, I decided that Elysia will be using TypeBox as a primary method, and might support Zod later as I've been focusing on some other areas at the moment.
TypeBox does the validation and also has Static Type Inference which made Eden happens by using Static Type Inference.
You can get type inference using the following code:
import { Elysia, t, type UnwrapSchema } from 'elysia'
const UserSchema = t.Object({
user: t.String()
})
type UserSchema = UnwrapSchema<typeof UserSchema>
Fun fact: I also prefer Zod over TypeBox on the client side.
Close as we have no plan to migrate to Zod in the near future.
Will re-open once we planned to migrate.
any future plan to consider Zod
as a type provider?
with Zod you can have the static type inference and a validator at the same time (https://zod.dev/)