DefinitelyTyped / DefinitelyTyped/DefinitelyTyped

[yup] InferType should default to nonRequired()

Open
#42,969 15 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
51.4k
Forks
30.4k
Avg merge
3d 21h
Merged PRs (30d)
108

Description

Issue
When creating a schema with Yup, you don't get validation errors on validate if a property is missing; it's non-required by default.

When using InferType<>, it marks all properties as required by default unless explicitly noted as notRequired(), which leads to TS errors in vscode

Sample

import {object, string, InferType} from 'yup'

const schema = object({
  requiredString: string().required(),
  notRequiredString: string()
})

// Some method that takes schema json as an argument
const method = (json: InferType<typeof schema>) => {}

// Error: Property 'notRequiredString' is missing in type '{ requiredString: string; }' but required in type (...)
method({
  requiredString: "done"
})

Expected
The above should show no error since notRequiredString isn't marked as required


  • Authors: @dhardtke @vtserman @MoretonBayRC @sseppola @YashdalfTheGray
    @vincentjames501 @robertbullen @sat0yu @deskoh @mauricedb @kalley

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the Yup TypeScript definitions for InferType and the object schema shown in the issue. Check how an unmarked string field is represented compared with requiredString, then add coverage demonstrating that the inferred notRequiredString property may be omitted while requiredString remains required.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
developer-experience
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.