Automattic / Automattic/mongoose

Pass through query type overrides in query chaining, so you don't have to include the computed type in the last function in the chain

Open
#11,710 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
27.5k
Forks
4k
Avg merge
2d 7h
Merged PRs (30d)
35

Description

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

**What is the current behaviour?**
Types do not take account of the type passed to populate.
**If the current behavior is a bug, please provide the steps to reproduce.**

```ts

const trip = await this.ds.model
.findOne(
{
_id: { $in: data.trips },
},
{
carrier: 1,
driver: 1,
},
)
.populate<{ driver: Pick }>('driver', 'name')
.lean<
Pick<
BookingTrip,
| 'carrier'
| '_id'
| 'driver'
>
>();

trip.driver // (property) driver?: string | Driver but it should be (property) driver: Pick
```

```json
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es2017",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"noEmitOnError": true,
"incremental": true,
"skipLibCheck": true,
"strictNullChecks": false,
"noImplicitAny": false,
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false,
"resolveJsonModule": true
}
}
```
**What is the expected behaviour?**
trip.driver should have the correct type `driver: Pick`

**What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.**
6.3.1

Contributor guide

Open the contributing guide

Research direction

Start with the TypeScript typings involved in the findOne().populate().lean() query chain, using the supplied reproduction to inspect how the populate generic is carried into lean(). Done means trip.driver is inferred as Pick rather than string | Driver.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.