Automattic / Automattic/mongoose

[Typscript] Wrong type for `new Model(doc)`

Open
#10,302 29 comments 0 reactions 0 assignees View on GitHub
developer-experience typescript
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*?**

bug

**What is the current behavior?**

When using `new Model(doc)` to create a new model and the `save()` it. the type of `doc` is always `any` which break the auto-completion for properties added by the user model.

![image](https://user-images.githubusercontent.com/32647899/119918891-9065e400-bf69-11eb-918d-cab2e65e34b5.png)

**If the current behavior is a bug, please provide the steps to reproduce.**

[Quick Start](https://mongoosejs.com/docs/index.html) example from the website

`tsconfig.json`
```json
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"moduleResolution": "node",
"types": ["mongoose"],
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"exclude": ["node_modules"],
"include": ["src/**/*.ts"]
}
```

**What is the expected behavior?**

instead of having `any` it should use the provided type.

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

nodejs: v16.1.0
mongoose: 5.12.10

Contributor guide

Open the contributing guide

Research direction

Start with the Quick Start example from the Mongoose website and the provided tsconfig.json, then reproduce the type of doc passed to new Model(doc) and checked after save(). Trace the TypeScript model and document typings to identify where any is introduced. Done means the provided model type is preserved and property autocomplete works without any.

Written by the indexing model from the issue text.

Assessment

Tech stack
mongodb, node.js, typescript
Domain
backend, 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.