New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
mongoose.InferSchemaType
does not correctly infer nullable arrays
#12420
Comments
The issue here has to do with In this case, that is a problem because the |
It appears to also be the issue with
gives
instead of
|
The same is true for nested schemas. They aren't optional, even though |
fix(types): make array paths optional in inferred type of array default returns undefined
@vkarpov15 I have the same issue as @hoangbn with array type (6.7.1 version):
|
Oh, I spent a lot of time to figure out that in order for |
Confirmed that this issue still happens if import mongoose from 'mongoose';
const TestSchema = new mongoose.Schema(
{
comments: { type: [String], default: () => undefined, required: false },
reference: {
type: [
{
type: { type: String, required: false },
value: { type: String, required: false },
},
],
default: () => undefined,
required: false,
},
},
{
collection: 'tests',
timestamps: false,
},
);
export type TestType = mongoose.InferSchemaType<typeof TestSchema>;
const doc: TestType = { comments: undefined }; |
It looks like InferSchemaType doesn't work very well without
I think for now we'll just add a note to use strict mode with inferred schema types. |
@vkarpov15 Yes, I also get However, it infers Without |
docs(typescript): make note about recommending `strict` mode when using auto typed schemas
Docs fix in #12825 |
Please this inferschemaType does not infer methods too |
@olawalejuwonm can you please open a new issue and follow the issue template? |
Prerequisites
Mongoose version
6.5.5
Node.js version
16.15.1
MongoDB server version
5.0.12
Description
When working with Mongoose schemas involving array fields that are nullable,
mongoose.InferSchemaType
does not seem to correctly infer the schema type. The notion of optionality does not seem to be reflected.Steps to Reproduce
Let's suppose we create a Mongoose schema with 2 fields of 2 different types, one being an array of strings, the other an array of objects, both optional.
In
TestType
, the notion of optionality of our fields seems to be lost:The problem is the same if we replace
default: () => undefined
bydefault: () => null
.If we just try
default: undefined
ordefault: null
, another problem appears:Perhaps there is a misunderstanding/error on my side in defining the schema. If this is the case, I would be happy to receive your advices. Otherwise, it must be a bug? 🐛
Expected Behavior
The text was updated successfully, but these errors were encountered: