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

Type issue with object, shape, and nullable fields #2060

Open
alikleitcr7 opened this issue Jul 16, 2023 · 0 comments
Open

Type issue with object, shape, and nullable fields #2060

alikleitcr7 opened this issue Jul 16, 2023 · 0 comments

Comments

@alikleitcr7
Copy link

Describe the bug
I have a next 13 application where I use formik along with yup, defining a schema inside an object for an interface does not seem to provide intellisense for the types inside as it does with the shape function.

Given the following interfaces:

interface Person {
  id: string;
  name: string | null;
}

interface PersonWithNullableName {
  id: string;
  name: string | null;
}

Using function object to define the schema does work on the variable by validating the schema but does not provide any type intellisense:

   // # NO TYPE INTELLISENSE . BUT WORKS 

  // try to remove a field, and click 'ctrl+space' to view type intellisense behind it
  // this will not provide intellisense  
  const personSchema: yup.ObjectSchema<Person> = yup.object({
    id: yup.string().required(),
    name: yup.string().required(),
  }).required();

Using function shape to define the schema DOES NOT work but DOES provide proper type intellisense:

  // # TYPE INTELLISENSE IS THERE, BUT DOES NOT WORK

  // now using shape with defining the interface, it will provide types intellisense
  // but we get the following error:
  // Type 'Person' does not satisfy the constraint 'ObjectShape'.
  // Index signature for type 'string' is missing in type 'Person'
  const personSchema2 = yup.object().shape<Person>({
    id: yup.string().required(),
  }).required();

Now having a nullable field inside of the interface, linter complains when defining as the following:


  // # NULLABLE FIELD ISSUE --> PersonWithNullableName interface

  // the field is null, so it should be working for nullable by adding 'nullable'
  // but it always complains with: Type 'undefined' is not assignable to type 'string | null' 
  // adding .default(null) seems to work, but why do we need to do that? or is it a bug?
  const personSchema3: yup.ObjectSchema<PersonWithNullableName> = yup.object({
    id: yup.string().required(),
    name: yup.string().nullable(),
  }).required();

To Reproduce

I created a repository under here and attached it to a codesandbox

Expected behavior
It's kind of ambiguous to me which one to use shape or object as in formik's documentation they use shape but in the current documentation you're using object. I think it should be clear which one to use and what are the differences in case any.

Both should be able to provide type intellisence and typescript should not complain for the shape with the error I mentioned.

For the nullable, I think it makes sense to be nullable without having to define .default(null) foreach field which can result in an ugly looking code (if that's not a bug already)

Additional context
Using next 13, app directory with src and pnpm package manager

@alikleitcr7 alikleitcr7 changed the title Type intellisense issue with object, shape, and nullable fields Type issue with object, shape, and nullable fields Jul 17, 2023
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