yup: Can't access properties defined by yup.InferType
Describe the bug Take for example this code
const personSchema = yup.object({
name: yup.string().required(),
age: yup.number().required()
});
type Person = yup.InferType<typeof personSchema>;
const me: Person = {
name: "Me",
age: 20
};
When I try to access me.name
or me.age
TypeScript reports an error saying
Property 'name' does not exist on type 'Id<Partial<Pick<undefined, never>> & Pick<undefined, never>> | Id<Partial<Pick<{ name: string; age: number; }, never>> & Pick<{ name: string; age: number; }, "name" | "age">>'.
Property 'name' does not exist on type 'Id<Partial<Pick<undefined, never>> & Pick<undefined, never>>'
To Reproduce I have written a codesanbox to demostrate the issue https://codesandbox.io/s/musing-breeze-pdx2p?file=/src/index.ts
Expected behavior
Being able to access me.name
and me.age
without a TypeScript error
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 16
- Comments: 15 (1 by maintainers)
@onursagir; @omid-ebrahimi ; @sudhagar10 ; @kunalpgithub - I don’t know if you found a fix for this but the following works for me.
I know this is closed. But I believe the fix here is to define the schema as required.
I.e. using the provided example
Removes any errors in my case. Hope this helps!
As a (temporary) fix you could use this
I got it to work using the solution described here: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/42969
In case anyone missed it, add
required()
at the end of the schema in addition to the fields that require it…I have the same problem. It was OK in version 0.28.1 but it fails in the current version.
I have similar problem. If I put the required after the shape, I got any type, which is not good. If I put after object and before shape, I have the rude never at he beginning of type. React-hooks-form is beepeing about it. PS: Leithal3`s solution solved the problem, I removed the shape part of yup constant and put the shape directly into the object.
You probably put required at the end of your schema definition. Try placing it straight after
object()
.@tbntdima did
required()
fix it for you or did it just change the type toany
? It changed the type toany
for me and that’s why the error went away.