Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Describe does not work well with union types #1229

Open
BoveFabio opened this issue Apr 25, 2024 · 0 comments
Open

Describe does not work well with union types #1229

BoveFabio opened this issue Apr 25, 2024 · 0 comments

Comments

@BoveFabio
Copy link

Hi everyone,

I stumbled across a seemingly simple example where Describe does not work well with union. However, it might be I am missing something.

Consider the following:

type A = {
    a: "a";
};
type B = {
    b: "b";
};

const aStruct: Describe<A> = object({
    a: literal("a"),
});
const bStruct: Describe<B> = object({
    b: literal("b"),
});
const aorbStruct: Describe<A | B> = union([aStruct, bStruct]);

I would expect this to work. However, aorbStruct shows a TS error:

TS2322: Type Struct<A | B, null> is not assignable to type Describe<A | B>
  Types of property schema are incompatible. 
    Type null is not assignable to type
      {
        a: Describe<"a">; 
      } | {
        b: Describe<"b">;
      }

The strange thing is that it works once I include a primitive in the union:

const AorBorNumberStruct: Describe<A | B | number> = union([aStruct, bStruct, number()]);

works fine.

Could you please

  • either help me out and show me how to get the const aorbStruct: Describe<A | B> = union([aStruct, bStruct]); case working
  • or confirm that this is bug?

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant