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.

yup.string().required() results in string | undefined

See original GitHub issue

Describe 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:closed
  • Created 2 years ago
  • Reactions:47
  • Comments:8

github_iconTop GitHub Comments

8reactions
neongreencommented, Jan 24, 2022

I think this is fixed in 1.0.0-beta.

7reactions
DASPRiDcommented, Sep 25, 2021

I ended up just switching to zod, it is definitely geared more towards TypeScript 😃

Read more comments on GitHub >

github_iconTop 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 >

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