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)

Most upvoted 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

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!

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>

I got it to work using the solution described here: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/42969

export type RemoveIndex<T> = {
  [K in keyof T as string extends K ? never : number extends K ? never : K]: T[K];
};

type FieldForm = RemoveIndex<Yup.InferType<typeof validationSchema>>;

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.

required() fixed it for me, but can someone explain why?

@tbntdima did required() fix it for you or did it just change the type to any? It changed the type to any for me and that’s why the error went away.

You probably put required at the end of your schema definition. Try placing it straight after object().

required() fixed it for me, but can someone explain why?

@tbntdima did required() fix it for you or did it just change the type to any? It changed the type to any for me and that’s why the error went away.