Cannot omit optional parameter
See original GitHub issueDescribe the bug When I define an object with optional parameters, I expect the type inferred from the object to allow missing fields.
To Reproduce
import * as yup from 'yup';
import Axios from "axios";
const idSchema = yup.object({
id: yup.string(),
});
type IdSchema = yup.TypeOf<typeof idSchema>;
interface Id2 {
id?: string;
}
const id1: IdSchema = {}; // Property 'id' is missing in type '{}' but required in type 'TypeOfShape<{ id: StringSchema<string | undefined, Record<string, any>, string | undefined>; }>'
const id2: Id2 = {}; // OK
const id3: IdSchema = { id: undefined }; // OK
Expected behavior allow missing fields. I don’t know if this is expected, but if you have the majority fields of a schema to be optional, it is very verbose to explicitly note them to be undefined
Platform (please complete the following information):
- Browser [e.g. chrome, safari] node
- Version [e.g. 22] yup 0.32.8, typescript 4.1.2
Additional context Add any other context about the problem here.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:13
- Comments:7 (1 by maintainers)
Top Results From Across the Web
How to pass optional parameters while omitting some other ...
The question is, as I see it, how one can bypass/skip several optional parameters and set only specific ones. country isn't required, it's...
Read more >Omit one optional parameter while providing another in TS
To omit one optional parameter, while providing another in a TypeScript function, pass an undefined value for the optional parameter you want to...
Read more >Named and Optional Arguments - C# Programming Guide
Named and optional arguments enable you to omit the argument for an optional parameter if you don't want to change the parameter's default...
Read more >Chapter 4 Labeled arguments - OCaml
Note that if that argument is labeled, you will only be able to eliminate optional arguments by totally applying the function, omitting all...
Read more >Optional Parameters - TADS
If the caller supplies a value for an optional parameter, the value is assigned to the parameter variable as usual. But if the...
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 Free
Top 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
I wrote my own type inference compatible with yup 0.32.8:
Now you can omit key in your value against the inferred type:
My little workaround: