Automattic / Automattic/mongoose

Remove FlattenMaps due to issues with private and protected fields

Open
#13,523 9 comments 0 reactions 0 assignees View on GitHub
discussion typescript
Dominant language
JavaScript
Stars
27.5k
Forks
4k
Avg merge
2d 7h
Merged PRs (30d)
35

Description

### Prerequisites

- [X] I have written a descriptive issue title
- [X] I have searched existing issues to ensure the bug has not already been reported

### Mongoose version

7.3.0

### Node.js version

16

### MongoDB server version

v18.16.0

### Typescript version (if applicable)

5.1.3

### Description

We have recently upgrade Mongoose from v7.0.3 -> v7.3.0 and noticed that calls to lean are now returning the Model type wrapped with FlattenMaps.

For example we we previously had a model typed in the following way -

```
// Model
const document: Model;

// Example Repository Usage
public async getById(id: string): Promise {
const quote = await this.document .findById(id).lean().exec();

// This used to be of type Quote but now FlattenMaps and causes a Typescript error
return quote;
}

```

Since v7.3.0 we have over 100 typescript errors complaining -
```
// Quote her is just an example it can be replaced with T
Type '(FlattenMaps & Required<{ _id: string; }>)[]' is not assignable to type 'Quote[]'
```

To enable us to build we to do the following -
```
// Model
const document: Model;

// Example Repository Usage
public async getById(id: string): Promise {
const quote = await this.document .findById(id).lean().exec();

// This would be of type Quote now
return quote;
}

```

### Steps to Reproduce

Please see description above

### Expected Behavior

The expected behaviour is that calls to to lean with be type to the T used by model unless overridden like I have done in my example above.

Contributor guide

Open the contributing guide

Research direction

Start by tracing the lean() type declarations and the FlattenMaps type used by Model in the Mongoose TypeScript definitions. Reproduce the example with TypeScript 5.1.3 and verify that lean() infers the model's T type by default while preserving the explicit lean() override.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js, typescript
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.