-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
[help] InferSchemaType, accessing _id field #11967
Comments
@philiparvidsson Thanks for providing this issue. |
@philiparvidsson can you please provide some code samples? That would be helpful for me to understand what you're trying to do. |
I added some example code, providing a brief example of the issue as well as the workaround I'm using. |
@philiparvidsson InferSchemaType meant to be a helper to infer schema defenition type. Can you please provide more details to be able to help maybe? |
In this particular case, I'm passing a MySchemaType_Problematic to a factory function. If the factory cannot generate the needed type, it needs to log that it did not understand the data it received. So I tried to do: log.error( But it doesn't work since the type doesn't have an id. I guess I could cast it to any because I know for a fact that _id is there, but that defeats the purpose of having any type on it to begin with. |
Another thing that I suspsect is a bug: Setting the type of a field (in the schema) to mongoose.Schema.Types.ObjectId gives the wrong type in the inferred type. E.g., const mySchema = ...({ _id: mongoose.Schema.Types.ObjectId }) const a: MyType = { _id: new mongoose.Types.ObjectId("...") } Fails, because it is expecting to be assigned a mongoose.schema.Types.ObjectId (which is the "metatype" here, so there is no way I can use that type for assigning anything). TL;DR: mongoose.Schema.Types.ObjectId should be inferred to mongoose.Types.ObjectId for fields |
From OP, I'd recommend doing: type MySchemaType_Workaround = HydratedDocument<InferSchemaType<typeof mySchema>> For "TL;DR: mongoose.Schema.Types.ObjectId should be inferred to mongoose.Types.ObjectId for fields", this was fixed in 6.4.2 with #12007. Those two issues are fixed, and there aren't enough code samples for this to be actionable. So I'm going to close this. Please re-open with a complete code sample that we can run to demonstrate the issue you're seeing, if you're still having issues. |
Prerequisites
Mongoose version
6.4.0
Node.js version
18.3
MongoDB version
Unsure
Operating system
Linux
Operating system version (i.e. 20.04, 11.3, 10)
arch-5.18.5 something
Issue
Using
InferSchemaType
to create the schema interface type, how do I access the_id
field? If I put it in the schema definition, I have to also set it explicitly when inserting new documents. If I don't put it there,InferSchemaType
does not include it in the interface.What do I do? I'd like to know the same for
.save()
and so on. Am I supposed to do a type union?Code:
The text was updated successfully, but these errors were encountered: