jquense / yup

Dead simple Object schema validation
MIT License
22.93k stars 934 forks source link

Array of Object Schema Infers Optional in Typescript #2154

Closed ridhlab closed 8 months ago

ridhlab commented 10 months ago

Describe the bug

I have schema

const schema = yup.object().shape({
    name: yup.string().required(getRequiredMessage("Name")),
    dataInput: yup
        .array()
        .of(
            yup.object().shape({
                variableInputId: yup
                    .number()
                    .required(getRequiredMessage("Variable Input")),
                value: yup.number().required(getRequiredMessage("Value")),
            })
        )
        .required(),
});

I expect the infers type of this schema is

{
    name: string,
    dataInput: { variableInputId: number, value: number }[]
}

But what I get is

{
    name: string,
    dataInput: { variableInputId?: number, value?: number }[]
}

I don't know why property inside object of array become optional instead of required. Is something wrong ?

yup version : ^1.3.3 typescript version : ^5.2.2

TVermaas commented 10 months ago

Experiencing the same issue with typescript 5.1.3 and yup 1.3.3.

tungto commented 9 months ago

I have same issue with yup 1.3.3

jquense commented 8 months ago

This works for me:

image

Please provide a runnable repro using the template