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.

Optional subfields get marked as required with InferType

See original GitHub issue

Describe the bug When when an object schema is created, it’s Typescript types are faithfully inferred via InferType, but when that same schema is included as a nested schema within another object, the resulting inferred types incorrectly drop the optional status of any nested fields.

To Reproduce

Minimal repro here: https://codesandbox.io/s/yup-infertype-nested-optional-issue-v63dd

import { object, string, InferType } from "yup";

const nameSchema = object({
  first: string().notRequired(),
  last: string().notRequired()
});

type NameType = InferType<typeof nameSchema>;

const userSchema = object({
  name: nameSchema.notRequired()
});

type UserType = InferType<typeof userSchema>;

class User implements UserType {
  name?: NameType;
}

Results in error:

Property 'name' in type 'User' is not assignable to the same property in base type 'Id<Partial<Pick<{ name: { first: ...; last: ...; }; }, "name">> & Pick<{ name: { first: ...; last: ...; }; }, never>>'.
  Type 'Id<Partial<Pick<{ first: string; last: string; }, "first" | "last">> & Pick<{ first: string; last: string; }, never>>' is not assignable to type '{ first: string; last: string; }'.
    Property 'first' is optional in type 'Id<Partial<Pick<{ first: string; last: string; }, "first" | "last">> & Pick<{ first: string; last: string; }, never>>' but required in type '{ first: string; last: string; }'.ts(2416)

Based on the hints, NameType by itself is correct:

type NameType = {
    first?: string;
    last?: string;
}

but once included in UserType, the optionals get dropped:

type UserType = {
    name?: {
        first: string;
        last: string;
    };
}

Expected behavior Optional state of fields is preserved for tested schemas

Platform (please complete the following information):

  • Browser [e.g. chrome]
  • Version [e.g. 83]

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

10reactions
taschmidtcommented, Jun 1, 2021

Now that yup comes with its own types, I’m seeing this issue again:

image

3reactions
taschmidtcommented, Jan 3, 2022

I think I just worked around it. @jquense maybe time to reopen this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Yup Infertype doesn't work for optional fields - Stack Overflow
In the current version of Yup we have solved it using Yup.SchemaOf<Type> : type User = { name: string age: number email?: string...
Read more >
Customizing the GraphQL Schema - Gatsby
The optional from argument allows getting the field on the current type which acts as the foreign-key to the field specified in by...
Read more >
apispec - Read the Docs
Optional marshmallow support class CategorySchema(Schema): id = fields.Int() name = fields.Str(required=True) class PetSchema(Schema):.
Read more >
Ubuntu Manpage: stap - systemtap script translator/driver
This SCRIPT is run in addition to the main script specified, through -e, or as a script file. This option can be repeated...
Read more >
https://www.nist.gov/tac/2014/KBP/ColdStart/tools/...
I # needed this to work on a machine without a great deal of memory, ... all of the problems that have been...
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