TypeScript rejects the is/then/otherwise object in Yup when()
'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.
Fix it fast
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/2Match what you see, then jump to its fix
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 --noEmitConfirmed by github.com
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 --noEmitConfirmed by github.com
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
TypeScript refuses JSX because the jsx option is not set
TS17004: Cannot use JSX unless the '--jsx' flag is provided.
TypeScript compiles JSX only when you name the file with a .tsx extension and enable the jsx compiler option.
2 causes, each with a fixWeb apps
TypeScript rejects a component that returns an array or other non-Element
Its return type 'Element[]' is not a valid JSX element.
TypeScript before 5.1 required every component to return JSX.Element or null, so arrays, strings and Promises were rejected even though React renders them.
2 causes, each with a fixWeb apps
ESLint says it cannot resolve a path to a module that does exist
Unable to resolve path to module 'react'. import/no-unresolved
import/no-unresolved fails when eslint-plugin-import cannot find the file or package on disk with its resolver.
4 causes, each with a fixWeb apps
ESLint reports no-unused-vars for React components and imports that are used
no-unused-vars reported for a component that is rendered as a JSX tag
ESLint's core no-unused-vars does not see variables used only as JSX tags, so a component imported and rendered as <Hello /> looks unused.
3 causes, each with a fixWeb 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 fixWeb apps
The renderInput prop stopped working on the MUI X DatePicker
The custom TextField (variant, size, helperText) is ignored
renderInput was removed from the MUI X date pickers in version 6.
3 causes, each with a fix