yup.string().required() results in string | undefined
See original GitHub issueDescribe the bug
When marking a string (or any other type) as .required()
, the result of a cast, according to TypeScript, is string | undefined
instead of the exepcted string
(as it was previously the case with definitely typed typings). Current workaround I use is to define an empty default.
To Reproduce
const schema = yup.object({
test: yup.string().required(),
}).required();
const data = schema.cast({test: 'foo'});
// This will throw an error that `string | undefined` is not assignable to `string`.
const test : string = data.test;
Expected behavior
required()
and defined()
should both exclude the undefined
type.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:47
- Comments:8
Top Results From Across the Web
Yup.string.when will always give the result undefined. How ...
I think you forgot to apply quotes ('') around iscountryCode field. Try this - let schema = Yup.object().shape({ zip: ...
Read more >How to use the yup.mixed function in yup - Snyk
To help you get started, we've selected a few yup.mixed examples, based on popular ... export const UpdateUserSchema = object({ username: string() .min(3, ......
Read more >Yup | Best of JS
import { object, string, number, date, InferType } from 'yup'; ... or null as a value. required negates the effects of calling optional()...
Read more >yup - npm
Yup separates the parsing and validating functions into separate steps. cast() transforms data while validate checks that the input is the ...
Read more >yup.DateSchema.required JavaScript and Node.js ... - Tabnine
DateSchema.required(Showing top 11 results out of 315) ... async store(req, res) { const schema = Yup.object().shape({ title: Yup.string().required(), ...
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 think this is fixed in 1.0.0-beta.
I ended up just switching to
zod
, it is definitely geared more towards TypeScript 😃