question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't access properties defined by yup.InferType

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:15
  • Comments:13 (1 by maintainers)

github_iconTop GitHub Comments

27reactions
Leithal3commented, Jul 13, 2020

@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

const personSchema = Yup.object({
  name: Yup.string().required(),
  age: Yup.number().required()
}).required()

type Person = Yup.InferType<typeof personSchema>;

const me: Person = {
  name: 'Me',
  age: 20
}

Removes any errors in my case. Hope this helps!

8reactions
onursagircommented, Jun 20, 2020

As a (temporary) fix you could use this

const personSchema = yup.object({
  name: yup.string().required(),
  age: yup.number().required()
});

type Person = ReturnType<typeof personSchema.validateSync>
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found