Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

TypeScript rejects the is/then/otherwise object in Yup when()

Error'is' does not exist in type 'ConditionBuilder<StringSchema<string, AnyObject, undefined, "">>'

Short answer

In Yup 1.x the object form of when() needs then and otherwise to be functions that receive the schema and return a schema. Passing a schema directly, or an old callback signature, no longer type-checks. Rewrite the branches as (schema) => schema.something(), or use the callback form with an array of values.

  • Applies to Yup 1.x, TypeScript 5.x, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find every when() call and look at what is given to then and otherwise. A schema there is the usual culprit.

    grep -rn "\.when(" -A5 src/
  2. 2Match what you see, then jump to its fix

    1. 1The error points at the is key of the object passed to when()then / otherwise are schemas, not functionsFix
    2. 2Type errors on the callback parameters, or the condition always takes the same branchThe callback form still uses the pre-1.0 signatureFix

What does the error look like?

  • 'is' does not exist in type 'ConditionBuilder<StringSchema<string, AnyObject, undefined, "">>'

Cause 1 of 2

then / otherwise are schemas, not functions

1Check

The Yup changelog says the builder-object form of when() requires then and otherwise to be functions of the form (schema: Schema) => Schema. Older code passed a schema directly.

grep -rn "then:" src/

2Fix

Turn each branch into a function that takes the current schema and returns a changed one.

// before
field: string().when('kind', { is: 'a', then: string().required() })

// after
field: string().when('kind', {
  is: 'a',
  then: (schema) => schema.required(),
  otherwise: (schema) => schema.notRequired(),
})

3Verify

Run the type checker.

npx tsc --noEmit

Confirmed by github.com

Did this fix it?

Cause 2 of 2

The callback form still uses the pre-1.0 signature

1Check

The docs show the callback form receiving an array of the dependent values and the schema. The changelog adds that this is no longer set to the schema and that every function must return a schema.

grep -rn "\.when(" src/ | grep "=>"

2Fix

Destructure the first parameter as an array and return a schema on every path.

count: number().when('isBig', ([isBig], schema) => {
  return isBig ? schema.min(5) : schema.min(0);
})

3Verify

Run the type checker and your validation tests.

npx tsc --noEmit

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • After a Yup major upgrade, read the changelog entries for when() before the first build.
  • Return a schema from every function you pass to when().

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

TypeScript rejects MUI Data Grid columns that set a type

A TypeScript error inside a column's renderCell callback

A columns array declared without a type is inferred with type as a plain string, which the Data Grid's column type union does not accept.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.