'Mongoose, TypeScript, WebStorm: typing on new Model
I am following Mongoose TypeScript documentation and using newest version of WebStorm (2022.1).
I am not getting the typing completions / checking that I would have expected when using new. For example:
user.model.ts
import { Schema, model } from 'mongoose';
export interface IUser {
userName: string,
password: string
}
const userSchema = new Schema<IUser>({
userName: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true,
},
});
export const User = model("user", userSchema);
test-user.ts
import { User } from 'user.model';
// "test" #1 - typing for new object creation (fails!)
const new_user = new User({
username: 'user2', // observe casing -- should get an error for username vs userName
password: 'badpw'});
// "test" #2 - typing for findOne (works!)
const find_user = User.findOne({userName: 'hello'}); // on the other hand typing does work here
In the first case (creating new User), I don't get any typing error on username vs userName and also get no intelligent autocompletion for construction the new object (i.e. suggesting userName or password as fields).
But given that the second case (findOne) works, maybe this is a WebStorm issue? If that's the case, are there any workarounds?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
