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 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.
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.