question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

InferSchemaType with instance methods

See original GitHub issue

Prerequisites

  • 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:closed
  • Created a year ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
heilmelacommented, Jun 30, 2022

@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:


UserModel.findOne()
  .exec()
  .then((x) => {
    //  (parameter) x: (mongoose.Document<unknown, any, {
    //     name: string;
    //     email: string;
    //     avatar?: string | undefined;
    // }> & {
    //     name: string;
    //     email: string;
    //     avatar?: string | undefined;
    // } & {
    //     _id: mongoose.Types.ObjectId;
    // } & {
    //     ...;
    // }) | null
    if (x !== null) x.test('a');
  });
1reaction
PeterPanencommented, Jun 29, 2022

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 🥳

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found