Optional subfields get marked as required with InferType
See original GitHub issueDescribe 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:
- Created 3 years ago
- Comments:9 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Now that yup comes with its own types, I’m seeing this issue again:
I think I just worked around it. @jquense maybe time to reopen this?