DZakh / rescript-schema

🧬 The fastest parser in the entire JavaScript ecosystem with a focus on small bundle size and top-notch DX
MIT License
162 stars 7 forks source link

Allow 'overwriting' fields when using 'merge' on schemas #95

Open justin0mcateer opened 2 months ago

justin0mcateer commented 2 months ago

The relatively new 'merge' function is very helpful. Thanks for adding that.

We often use the 'merge' function to produce more specific versions of some base type. In this case, it's helpful to be able to narrow certain types in the result of the intersection/merge.

As an example:

type Base = { type: 'foo' | 'bar', name: string, }

type Foo = { type: 'foo', name: string, fooCount: number, }

type Bar = { type: 'bar', name: string, barSize: 'small' | 'large', }

Currently, if a field exists in both types provided to 'merge', it throws an error that 'type' is already defined. In Zod, the Schema on the right overrides the Schema on the left. This also allows for creating some type of derived schema where one or two properties are different.

DZakh commented 2 months ago

I'll investigate.