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

Compilation error when specifying an object with any field #2013

Closed
Code-Hex opened this issue May 27, 2023 · 5 comments
Closed

Compilation error when specifying an object with any field #2013

Code-Hex opened this issue May 27, 2023 · 5 comments

Comments

@Code-Hex
Copy link

Describe the bug

I get a compile error when I specify an object with any field. If this is the intended behavior, I would appreciate ideas on how to resolve this.

Type 'ObjectSchema<{ inner: { testScalar?: any; }; }, AnyObject, { inner: any; }, "">' is not assignable to type 'ObjectSchema<OuterType, AnyObject, any, "">'.
  The types of 'default(...).fields.inner' are incompatible between these types.
    Type 'Reference<unknown> | ISchema<{ testScalar?: any; }, AnyObject, any, any>' is not assignable to type 'Reference<unknown> | ISchema<InnerType, AnyObject, any, any>'.
      Type 'ISchema<{ testScalar?: any; }, AnyObject, any, any>' is not assignable to type 'Reference<unknown> | ISchema<InnerType, AnyObject, any, any>'.
        Type 'ISchema<{ testScalar?: any; }, AnyObject, any, any>' is not assignable to type 'ISchema<InnerType, AnyObject, any, any>'.
          Type '{ testScalar?: any; }' is not assignable to type 'InnerType'.
            Property 'testScalar' is optional in type '{ testScalar?: any; }' but required in type 'InnerType'.ts(2322)

To Reproduce

Here is a minimal reproduction code that is based on your code:

import * as yup from "yup";

type InnerType = {
  testScalar: any;
};

type OuterType = {
  inner: InnerType;
};

function InnerTypeSchema(): yup.ObjectSchema<InnerType> {
  return yup.object({
    testScalar: yup.mixed()
  });
}

function OuterTypeSchema(): yup.ObjectSchema<OuterType> {
  return yup.object({
    inner: InnerTypeSchema()
  });
}

https://codesandbox.io/s/inspiring-beaver-gu3m0f?file=/src/index.test.ts

Expected behavior

compilable

Platform (please complete the following information):

  • TypeScript v5.0.4

Additional context

Code-Hex/graphql-codegen-typescript-validation-schema#359 (comment)

@jquense
Copy link
Owner

jquense commented May 27, 2023

The type error is telling you what the problem is. You have a type that isn't compatible with the schema you wrote. Specifically your type wants testScalar to be required but your schema it's optional.

@jquense jquense closed this as completed May 27, 2023
@Code-Hex
Copy link
Author

Code-Hex commented May 27, 2023

@jquense Thanks.

Specifically your type wants testScalar to be required but your schema it's optional.

I tried with this code but I got the same error (sure, I wrote "strict": true in tsconfig.json):

function InnerTypeSchema(): yup.ObjectSchema<InnerType> {
  return yup.object({
    testScalar: yup.mixed().required()
    //     testScalar: yup.mixed().defined() also error
  });
}
Type 'ObjectSchema<{ inner: { testScalar?: any; }; }, AnyObject, { inner: any; }, "">' is not assignable to type 'ObjectSchema<OuterType, AnyObject, any, "">'.
  The types of 'default(...).fields.inner' are incompatible between these types.
    Type 'Reference<unknown> | ISchema<{ testScalar?: any; }, AnyObject, any, any>' is not assignable to type 'Reference<unknown> | ISchema<InnerType, AnyObject, any, any>'.
      Type 'ISchema<{ testScalar?: any; }, AnyObject, any, any>' is not assignable to type 'Reference<unknown> | ISchema<InnerType, AnyObject, any, any>'.
        Type 'ISchema<{ testScalar?: any; }, AnyObject, any, any>' is not assignable to type 'ISchema<InnerType, AnyObject, any, any>'.
          Type '{ testScalar?: any; }' is not assignable to type 'InnerType'.
            Property 'testScalar' is optional in type '{ testScalar?: any; }' but required in type

I hope we can reopen this issue 🙏

@Code-Hex
Copy link
Author

Code-Hex commented May 27, 2023

I think yup.ObjectSchema is probably buggy somehow because the error stops when I write it this way.

function InnerTypeSchema() {
  return yup
    .object({
      testScalar: yup.mixed().defined() // also .required()
    })
}

@jquense
Copy link
Owner

jquense commented May 27, 2023

It's not buggy, ObjectSchema has a lot of generics, that aren't filled in when you just use ObjectSchema<InnerType>, you can see that the inferred return type of the function is slightly different than just ObjectSchema<InnerType>. I would recommend matching it exactly or not including it. IF you want to ensure that it it's at least ObjectSchema<InnerType> use:

function InnerTypeSchema() {
  return yup.object({
    testScalar: yup.mixed().defined()
  }) satisfies yup.ObjectSchema<InnerType>;
}

@Code-Hex
Copy link
Author

@jquense I got it. satisfies is a good answer for me!
Thank you so much!

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

2 participants