Skip to content

Conversation

JstnMcBrd
Copy link
Contributor

Summary

While using Mongoose with TypeScript, @ShunnShine and I discovered the dangers of defining the schema in a separate variable when we realized all of our fields with required: false were being treated as required: true.

We eventually figured out how to fix the type definitions by using as const.

We looked at the Schemas in TypeScript documentation and saw a warning about assigning a schema to a temporary variable (fair enough), but did not see any information about using as const, even though it is used in the example code right below - without explanation.

This PR adds a little section about type widening and as const to help users understand why this was done.

@hasezoey hasezoey added the docs This issue is due to a mistake or omission in the mongoosejs.com documentation label Aug 6, 2024
Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

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

Thanks 👍

@vkarpov15 vkarpov15 added this to the 8.5.3 milestone Aug 7, 2024
@vkarpov15 vkarpov15 merged commit 8575b9c into Automattic:master Aug 7, 2024
@JstnMcBrd JstnMcBrd deleted the mcb/as-const branch August 8, 2024 04:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants