Populate type with TypeScript not working
See original GitHub issueDo 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:
- Created 2 years ago
- Reactions:3
- Comments:18 (6 by maintainers)
Top 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 >
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 Free
Top 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
@vkarpov15 I’m running in the same issue as @judgegodwins regarding the populate method returning an intersection type:
The type of
doc.child
in the callback isTypes.ObjectId & Child
, while we expectChild | null
. (iirc thenull
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
staysTypes.ObjectId & Child
.Typescript 4.2.4 / Mongoose 6.0.13
@thiagokisaki using your example above, there’s still no
name
property onchild
.