Yup: InferType fails on nested optional types

Created on 25 Jul 2019  路  4Comments  路  Source: jquense/yup

First of all, great work guys! I love using yup!!

I did however encounter an issue with InferType as shown below:

Steps to reproduce:

export const FullAddressValidation = object({
  placeId: string().required(),
  apartment: string()
    .nullable()
    .notRequired(),
});
export type FullAddresValidationType = InferType<typeof FullAddressValidation>;

export const UserDetailsValidation = object({
  address: FullAddressValidation,
});

export type UpdateUserValidationType = InferType<typeof UserDetailsValidation>;

Since apartment is optional, the direct infertype works making the field optional in the type.
In the nested case however, its given the type of null | undefined, but the field is no longer optional.

First image shows the direct type:

image

Second image shows how the optional field is removed

image.

Most helpful comment

I would also love to echo the "great work" comment. This is so much better than trying to get joi-browser to work. We are having the same issue described above, in a slightly different use case:

const FooSchema = yup.object().shape({
    bar: yup.string().notRequired(),
});

class Foo implements yup.InferType<typeof FooSchema> {
    public static schema = FooSchema;
    public bar?: string;
}

const BarSchema = yup.object().shape({
    foos: yup.array().of(Foo.schema),
});

class Bar implements yup.InferType<typeof BarSchema> {
    public foos: Foo[];
}

Here's a screenshot of the error:

image

The error goes away if we change the bar property definition to the following:

note: removed the ? flag and added | undefined

public bar: string | undefined;

All 4 comments

I would also love to echo the "great work" comment. This is so much better than trying to get joi-browser to work. We are having the same issue described above, in a slightly different use case:

const FooSchema = yup.object().shape({
    bar: yup.string().notRequired(),
});

class Foo implements yup.InferType<typeof FooSchema> {
    public static schema = FooSchema;
    public bar?: string;
}

const BarSchema = yup.object().shape({
    foos: yup.array().of(Foo.schema),
});

class Bar implements yup.InferType<typeof BarSchema> {
    public foos: Foo[];
}

Here's a screenshot of the error:

image

The error goes away if we change the bar property definition to the following:

note: removed the ? flag and added | undefined

public bar: string | undefined;

Ran to this issue, any ideas how to fix or workaround?

Why was this issue closed? I too am having this issue.

image
image

Types aren't maintained here. please direct type feedback to DT and whoever is maintaining them

Was this page helpful?
0 / 5 - 0 ratings