elysiajs / elysia

Ergonomic Framework for Humans
https://elysiajs.com
MIT License
10.46k stars 223 forks source link

i recommend the usage of zod instead of typebox #11

Closed Victugord closed 1 year ago

Victugord commented 1 year ago

with Zod you can have the static type inference and a validator at the same time (https://zod.dev/)

izakfilmalter commented 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>
SaltyAom commented 1 year ago

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.

SaltyAom commented 1 year ago

Close as we have no plan to migrate to Zod in the near future.

Will re-open once we planned to migrate.

MuhammadAli-208 commented 1 year ago

any future plan to consider Zod as a type provider?