You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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
The text was updated successfully, but these errors were encountered:
alikleitcr7
changed the title
Type intellisense issue with object, shape, and nullable fields
Type issue with object, shape, and nullable fields
Jul 17, 2023
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 theshape
function.Given the following interfaces:
Using function
object
to define the schema does work on the variable by validating the schema but does not provide any type intellisense:Using function
shape
to define the schema DOES NOT work but DOES provide proper type intellisense:Now having a nullable field inside of the interface, linter complains when defining as the following:
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
orobject
as in formik's documentation they useshape
but in the current documentation you're usingobject
. 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
andpnpm
package managerThe text was updated successfully, but these errors were encountered: