InferSchemaType with instance methods
See original GitHub issuePrerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the issue has not already been raised
Issue
So I wanna use the newly added InferSchemaType
to remove boilerplate interfaces. However im not sure how would one add InstanceMethods, Virtuals, etc. to the Schema without defining the now inferred DocType. When leaving EnforceDocType and Model as defaults, the inferred document type is { [x: string]: any] }
. Unfortunatly, typescript does not allow to skip generic parameters for now (https://github.com/microsoft/TypeScript/issues/10571)
// Schema
const schema = new Schema<
any,
mongoose.Model<any, any, any, any, any>,
{
test: () => void;
}
>({
name: { type: String, required: true },
email: { type: String, required: true },
avatar: String,
});
type User = InferSchemaType<typeof schema>;
// InferSchemaType will determine the type as follows:
// type User = {
// [x: string]: any;
// };
Issue Analytics
- State:
- Created a year ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Create mongoose schema methods using TypeScript
As suggested by one of the collaborators of mongoose, we can use the below way for creating instance methods:
Read more >Get Document type with QueryHelpers and InstanceMethods
Prerequisites. I have written a descriptive issue title. Mongoose version. 6.5.2. Node.js version. 17.3. MongoDB version.
Read more >Schemas in TypeScript - Mongoose
Mongoose uses the M type in model middleware defined in the schema. The third generic param, TInstanceMethods is used to add types for...
Read more >Why reflect-metadata suc*s - DEV Community
Traceur attaches annotations to a static property on the class. ... 1. the second one will instance the method function code every time...
Read more >PySpark schema inference and 'Can not infer schema for type ...
There is nothing you can do here except changing the instance creation method. Let's check the first one. Here, instead of using a...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@iammola i updated the comment, it was a false leftover from a previous edit where i used
find()
. You are absolutely correct the union with null did produce the error. This works:Wait i just realized that if i remove the
InferSchemaType
line in my example, and just let it infer automatically without me doing anything, it includes the instance methods 🥳