You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
By default, Mongoose automatically infers the shape of your documents based on your schema definition.
49
+
However, if you modify your schema after your `new Schema()` call (like with plugins) then Mongoose's inferred type may be incorrect.
50
+
For cases where Mongoose's automatic schema type inference is incorrect, you can define a raw document interface that tells Mongoose the type of documents in your database as follows.
15
51
16
52
```typescript
17
53
import { Schema, model, connect } from'mongoose';
@@ -67,8 +103,6 @@ const user: HydratedDocument<IUser> = new User({
67
103
});
68
104
```
69
105
70
-
## ObjectIds and Other Mongoose Types
71
-
72
106
To define a property of type `ObjectId`, you should use `Types.ObjectId` in the TypeScript document interface. You should use `'ObjectId'` or `Schema.Types.ObjectId` in your schema definition.
73
107
74
108
```ts
@@ -106,4 +140,4 @@ However, before you do, please [open an issue on Mongoose's GitHub page](https:/
106
140
107
141
## Next Up
108
142
109
-
Now that you've seen the basics of how to use Mongoose in TypeScript, let's take a look at [statics in TypeScript](typescript/statics-and-methods.html).
143
+
Now that you've seen the basics of how to use Mongoose in TypeScript, let's take a look at [methods in TypeScript](typescript/statics-and-methods.html).
You can define instance methods and static functions on Mongoose models.
4
-
With a little extra configuration, you can also register methods and statics in TypeScript.
5
-
6
-
## Methods
7
-
8
-
To define an [instance method](../guide.html#methods) in TypeScript, create a new interface representing your instance methods.
9
-
You need to pass that interface as the 3rd generic parameter to the `Schema` constructor **and** as the 3rd generic parameter to `Model` as shown below.
3
+
To use Mongoose's automatic type inference to define types for your [statics](../guide.html#statics) and [methods](../guide.html#methods), you should define your methods and statics using the `methods` and `statics` schema options as follows.
4
+
Do **not** use the `Schema.prototype.method()` and `Schema.prototype.static()` functions, because Mongoose's automatic type inference system cannot detect methods and statics defined using those functions.
10
5
11
6
```typescript
12
-
import { Model, Schema, model } from'mongoose';
13
-
14
-
interfaceIUser {
15
-
firstName:string;
16
-
lastName:string;
17
-
}
18
-
19
-
// Put all user instance methods in this interface:
20
-
interfaceIUserMethods {
21
-
fullName():string;
22
-
}
23
-
24
-
// Create a new Model type that knows about IUserMethods...
We recommend using Mongoose's automatic type inference where possible, but you can use `Schema` and `Model` generics to set up type inference for your statics and methods.
44
35
Mongoose [models](../models.html) do **not** have an explicit generic parameter for [statics](../guide.html#statics).
45
36
If your model has statics, we recommend creating an interface that [extends](https://www.typescriptlang.org/docs/handbook/interfaces.html) Mongoose's `Model` interface as shown below.
0 commit comments