question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Intersection cannot be assigned to `Schema` type

See original GitHub issue

Since 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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
colinhackscommented, Aug 17, 2021

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:

function parse<T extends z.ZodTypeAny>(schema: T): z.infer<T> { ... }
0reactions
geigerzaehlercommented, Aug 16, 2021

It should work for this use case.

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.

Everything that involves writing schemas the conform to pre-defined TS types is doing to be messy. It’s just not what Zod was really designed for.

Thanks for clarifying this. We would love to see this becoming easier though 🙂. @mojotech/json-type-validation does this well.

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found