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.

Populate type with TypeScript not working

See original GitHub issue

Do you want to request a feature or report a bug? bug

What is the current behavior? property in interface with type PopulatedDoc<interface & Document> has type any If the current behavior is a bug, please provide the steps to reproduce.

import mongoose, { Schema, model, Document, PopulatedDoc } from 'mongoose';
async function run() {
    mongoose.connect('mongodb://localhost:27017/test');
    // `child` is either an ObjectId or a populated document
    interface Parent {
        child?: PopulatedDoc<Child & Document>,
        name?: string
    }
    const ParentModel = model<Parent>('Parent', new Schema<Parent>({
        child: { type: 'ObjectId', ref: 'Child' },
        name: String
    }));

    interface Child {
        name?: string;
    }
    const childSchema: Schema = new Schema<Child>({ name: String });
    const ChildModel = model<Child>('Child', childSchema);

    const parent = await ParentModel.findOne({}).populate('child')
    parent!.child.name;
}
{
  "compilerOptions": {
    "target": "ES6",                      
    "module": "commonjs",                     
    "strict": true,                          
    "esModuleInterop": true,                 
    "forceConsistentCasingInFileNames": true 
  }
}

What is the expected behavior? it should support the type of given interface What are the versions of Node.js, Mongoose and MongoDB you are using? Note that “latest” is not a version. Node.js: 14.17.4 , Mongoose: 6.0.7 , MongoDB: 4.4.3

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:18 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
sheerloxcommented, Dec 19, 2021

@vkarpov15 I’m running in the same issue as @judgegodwins regarding the populate method returning an intersection type:

ParentModel.findOne({})
  .populate<{ child: Child }>('child')
  .orFail()
  .then((doc) => {
    const t: string = doc.child.name
  })

The type of doc.child in the callback is Types.ObjectId & Child, while we expect Child | null. (iirc the null type should be automatically added since Mongoose might return it if the document was not found).

Even when using .populate<{ child: Child | null }>('child'), doc.child stays Types.ObjectId & Child.

Typescript 4.2.4 / Mongoose 6.0.13

2reactions
ahmedelshenawy25commented, Sep 21, 2021

@thiagokisaki using your example above, there’s still no name property on child. image

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript - How to define model in combination with using ...
The issue I'm facing is that I don't know how to type my models so that a property can be either an ObjectId...
Read more >
Populate with TypeScript - Mongoose
Mongoose's TypeScript bindings add a generic parameter Paths to the populate() : import { Schema, model, Document, Types } from 'mongoose'; ...
Read more >
How To Create Custom Types in TypeScript - DigitalOcean
Though the basic types in TypeScript will cover many use cases, creating your own custom types based on these basic types will allow...
Read more >
The starting point for learning TypeScript
Find TypeScript starter projects: from Angular to React or Node.js and CLIs.
Read more >
TypeScript Compiling with Visual Studio Code
Step 1: Create a simple TS file · Step 2: Run the TypeScript build · Step 3: Make the TypeScript Build the default...
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