Skip to content

Conversation

@vkarpov15
Copy link
Collaborator

Fix #15878

Summary

Currently, defining a path as { type: String, default: undefined } will correctly treat the path as not required in TypeScript... but { type: String, default: null } will treat the path as required, which is incorrect.

Examples

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes TypeScript type inference for Mongoose schema paths with default: null. Previously, paths with default: null were incorrectly treated as required in TypeScript, while default: undefined correctly treated them as optional. The fix extends the type checking logic to treat both null and undefined defaults consistently as optional paths.

Key Changes:

  • Renamed IsPathDefaultUndefined to IsPathDefaultNullish to reflect expanded scope
  • Extended type checking to handle default: null and default: () => null patterns
  • Added comprehensive TypeScript test coverage for the new behavior

Reviewed changes

Copilot reviewed 1 out of 2 changed files in this pull request and generated 1 comment.

File Description
types/inferschematype.d.ts Renamed and expanded the IsPathDefaultNullish type utility to check for both null and undefined defaults (including function-based defaults), and updated all usage sites
test/types/schema.test.ts Added TypeScript test case gh15878() validating that paths with default: null and default: () => null are correctly inferred as optional types

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@hasezoey hasezoey added the typescript Types or Types-test related issue / Pull Request label Dec 14, 2025
Copy link
Collaborator

@hasezoey hasezoey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aside from the benchmark, looks good to me

@vkarpov15
Copy link
Collaborator Author

I was able to make some small improvements to reduce number of typescript instantiations, but not enough to get back down to 300k. Which is fine because the ts benchmark is failing on 8.x right now with 338k instantiations, and this PR knocks that down to 334k. Small improvement, and I increased the instantiations limit to make the benchmark pass.

@vkarpov15 vkarpov15 merged commit e68a378 into 8.x Dec 15, 2025
5 checks passed
@vkarpov15 vkarpov15 deleted the vkarpov15/gh-15878 branch December 15, 2025 15:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

typescript Types or Types-test related issue / Pull Request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants