Can't access properties defined by yup.InferType
See original GitHub issueDescribe 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
Issue Analytics
- State:
- Created 3 years ago
- Reactions:15
- Comments:13 (1 by maintainers)
Top Results From Across the Web
Yup InferType for array of strings not working as expected
Using InferType seems to work fine for strings and objects, but there's unexpected behavior for arrays. When required() or defined() is used ...
Read more >Unified Validation & Typings in Web-Apps using yup
So let's start off with a central place (a shared library for instance) where we'll define the schemas as well as the actual...
Read more >zod - npm
Otherwise Zod can't correctly infer the types of your schemas! ... Use .shape to access an object schema's property schemas.
Read more >Announcing TypeScript 4.7 - Microsoft Developer Blogs
If you've already read our beta or RC blog posts, you can read up on ... extends Constraints on infer Type Variables; Optional...
Read more >How to use typescript with Yup validation - Reddit
For instance, If a property was an string and now is a number, or the property is optional but now it is not,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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