Intersection cannot be assigned to `Schema` type
See original GitHub issueSince v3.3 it is not possible anymore to give an explicit type definition for schemas of intersection types.The following code fails to type check.
import * as zod from "zod"
interface Foo {
foo: string;
}
const fooSchema = zod.object({
foo: zod.string(),
});
interface Bar {
bar: string;
}
const barSchema = zod.object({
bar: zod.string(),
});
const interSchema: zod.Schema<Foo & Bar> = fooSchema.and(barSchema);
Typescript produces the following error
index.ts:19:14 - error TS2322: Type 'ZodIntersection<ZodObject<{ foo: ZodString; }, "strip", ZodTypeAny, { foo: string; }, { foo: string; }>, ZodObject<{ bar: ZodString; }, "strip", ZodTypeAny, { ...; }, { ...; }>>' is not assignable to type 'ZodType<Foo & Bar, ZodTypeDef, Foo & Bar>'.
The types returned by '_parse(...)' are incompatible between these types.
Type 'ParseReturnType<ZodObject<{ foo: ZodString; }, "strip", ZodTypeAny, { foo: string; }, { foo: string; }> & ZodObject<{ bar: ZodString; }, "strip", ZodTypeAny, { ...; }, { ...; }>>' is not assignable to type 'ParseReturnType<Foo & Bar>'.
Type 'OK<ZodObject<{ foo: ZodString; }, "strip", ZodTypeAny, { foo: string; }, { foo: string; }> & ZodObject<{ bar: ZodString; }, "strip", ZodTypeAny, { ...; }, { ...; }>>' is not assignable to type 'ParseReturnType<Foo & Bar>'.
Type 'OK<ZodObject<{ foo: ZodString; }, "strip", ZodTypeAny, { foo: string; }, { foo: string; }> & ZodObject<{ bar: ZodString; }, "strip", ZodTypeAny, { ...; }, { ...; }>>' is not assignable to type 'OK<Foo & Bar>'.
Type 'ZodObject<{ foo: ZodString; }, "strip", ZodTypeAny, { foo: string; }, { foo: string; }> & ZodObject<{ bar: ZodString; }, "strip", ZodTypeAny, { ...; }, { ...; }>' is not assignable to type 'Foo & Bar'.
Property 'foo' is missing in type 'ZodObject<{ foo: ZodString; }, "strip", ZodTypeAny, { foo: string; }, { foo: string; }> & ZodObject<{ bar: ZodString; }, "strip", ZodTypeAny, { ...; }, { ...; }>' but required in type 'Foo'.
19 const interSchema: zod.Schema<Foo & Bar> = fooSchema.and(barSchema);
~~~~~~~~~~~
A workaround is to use .merge
.
export const interSchema2: zod.Schema<Foo & Bar> = fooSchema.merge(barSchema);
However, this requires fooSchema
to be an object and is not possible if fooSchema
itself is annotated with Schema<Foo>
.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:8 (3 by maintainers)
Top Results From Across the Web
Type Intersection fail but Extending Interface works · Issue #359
We're composing types to make partial validation. Both generate schemas but Type Intersection schemas fail in AJV but extending an Interface ...
Read more >TypeScript Intersection (not Union) Type from JSON Schema
It seems as if FromSchema cannot handle a union of the schemas. You can get around this by turning the union into a...
Read more >Types — EdgeQL | EdgeDB Docs
Every object stored in the database is strongly and immutably typed; you can't simply convert an object to an object of a different...
Read more >CREATE SCHEMA - Amazon Redshift - AWS Documentation
Defines a new schema for the current database. ... The schema name can't be PUBLIC . For more information about valid names, see...
Read more >Template functions - arrays - Azure Resource Manager
The following example shows how to use the array function with different types. JSON Copy. { "$schema" ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Everything is easier when you don’t support chainable methods (a la https://github.com/mojotech/json-type-validation) but the fluent approach is one of Zod’s main selling points. It’s all tradeoffs.
Updated:
The signature you proposed doesn’t seem to match what we need. (I also noticed that I had a typo in the signature. I’ve fixed now). The idea of the generic
parse
function was to ensure that the type parsed by the schema matches the output type.Thanks for clarifying this. We would love to see this becoming easier though 🙂.
@mojotech/json-type-validation
does this well.