-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
schema type auto infer bugs #12030
Comments
@imranbarbhuiya Thanks for reporting this one as well, I will check the new changes and see what we can do 👍 |
I've updated code block 4 and added the 5th point which is the same as 1 but for an object. |
resulting to a type of
but it should be
|
Do you mean? type ISchema = {
track?: {
backupCount: number;
count: number;
};
} |
@imranbarbhuiya @shiva-prasad-reddy Thanks for being a part of improving auto inferred schema. Best regards. |
Hey, @mohammad0-0ahmad @shiva-prasad-reddy @vkarpov15 @imranbarbhuiya IMO, what @shiva-prasad-reddy is expecting is wrong. Based on the documentation, https://mongoosejs.com/docs/subdocs.html#altsyntaxsingle. If you want to specify the options for a schema path, you have to use a This is the example on the documentation. const schema = new Schema({
nested: {
// Do not do this! This makes `nested` a mixed path in Mongoose 5
type: { prop: String },
required: true
}
});
const schema = new Schema({
nested: {
// This works correctly
type: new Schema({ prop: String }),
required: true
}
}); So your code should be. const schema = new Schema({
track: new Schema({ // note the Schema constructor
backupCount: {
type: Number,
default: 0,
},
count: {
type: Number,
default: 0,
},
}),
}); Which will infer exactly what you're expecting type ISchema = {
track?: {
backupCount: number;
count: number;
} | undefined
} |
Thanks @iammola If I understood correctly, You mean that const schema = new Schema({
track: new Schema({ // note the Schema constructor
backupCount: {
type: Number,
default: 0,
},
count: {
type: Number,
default: 0,
},
}),
});
type Schema = inferSchemaType<typeof schema>;
// gives
// {
// track?: {
// backupCount: number;
// count: number;
// } | undefined
// } while the provided code by @shiva-prasad-reddy : const schema = new Schema({
track: {
backupCount: {
type: Number,
default: 0,
},
count: {
type: Number,
default: 0,
},
// Note: track path has none path options like type, setters or getters
},
});
type Schema = InferSchemaType<typeof schema>;
// gives
// {
// track?: any
// } Is that correct? |
Pretty much @mohammad0-0ahmad, But currently it doesn't give It should give this. type ISchema = {
track: {
backupCount: {
type: number;
default: number;
};
count: {
type: number;
default: number;
};
};
} |
@iammola Thanks again for your feedback. |
Is fixed in 6.4.5? |
Waiting for maintainer response. |
@iammola just to clarify. The correct TS type for the below schema:
should be:
as @shiva-prasad-reddy indicated. The info from https://mongoosejs.com/docs/subdocs.html#altsyntaxsingle is specific to Mongoose 5 and also specific to cases where |
…ested path declarations in Mongoose 5 Re: #12030
Ahh, I see @vkarpov15. My bad! |
Prerequisites
Mongoose version
6.4.2
Node.js version
18
MongoDB server version
5.0
Description
I've found a few more bugs in auto infer type
Array
instead ofTypes.DocumentArray
which makes pushing fields difficultSteps to Reproduce
added code blocks above
Expected Behavior
No response
The text was updated successfully, but these errors were encountered: