Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't access properties defined by yup.InferType #946

Closed
onursagir opened this issue Jun 19, 2020 · 15 comments
Closed

Can't access properties defined by yup.InferType #946

onursagir opened this issue Jun 19, 2020 · 15 comments

Comments

@onursagir
Copy link

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

@omid-ebrahimi
Copy link

I have the same problem. It was OK in version 0.28.1 but it fails in the current version.

@onursagir
Copy link
Author

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>

@sudhagar10
Copy link

Facing the same issue

@kunalpgithub
Copy link

I am also facing same issue. It looks like it appends ? to property name which is what makes it inaccessible

@jquense
Copy link
Owner

jquense commented Jul 4, 2020

Folks this isn't the right place to report type concerns, the types are maintained on DT by other ppl. Please raise them there

@jquense jquense closed this as completed Jul 4, 2020
@Leithal3
Copy link

@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!

@DoubleOTheven
Copy link

In case anyone missed it, add required() at the end of the schema in addition to the fields that require it...

@tbntdima
Copy link

tbntdima commented Sep 9, 2020

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

@DASPRiD
Copy link

DASPRiD commented Sep 21, 2020

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

The definitions were recently fixed – without the required() call, a schema can in theory return undefined.

@RVledder
Copy link

RVledder commented Sep 25, 2020

Oh man. This has cost me quite some hours. Thanks for this thread.

Now that I look closely, it ís in the Readme.md. Under the Typescript title it uses the .defined() method for the schema.

@jquense Should it perhaps deserve a comment in that example? (would be happy to contribute that change)

@codeBelt
Copy link

codeBelt commented Feb 3, 2021

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.

@ecklf
Copy link

ecklf commented Jul 8, 2021

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().

@teddybee
Copy link

teddybee commented Jul 22, 2022

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.

@rationalthinker1
Copy link

I got it to work using the solution described here:
DefinitelyTyped/DefinitelyTyped#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>>;

@justinechang39
Copy link

I got it to work using the solution described here: DefinitelyTyped/DefinitelyTyped#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>>;

This solution seems to be the best, when I use the .required() method, I seem to lose type checking on the errors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests