Skip to content

Commit d3d2ec4

Browse files
committed
docs(migrating_to_8): add note about #13897 to migration guide
1 parent 8d61a7d commit d3d2ec4

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

docs/migrating_to_8.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ If you're still on Mongoose 6.x or earlier, please read the [Mongoose 6.x to 7.x
2525
* [`create()` waits until all saves are done before throwing any error](#create-waits-until-all-saves-are-done-before-throwing-any-error)
2626
* [`Model.validate()` returns copy of object](#model-validate-returns-copy-of-object)
2727
* [Allow `null` For Optional Fields in TypeScript](#allow-null-for-optional-fields-in-typescript)
28+
* [Model constructor properties are all optional in TypeScript](#model-constructor-properties-are-all-optional-in-typescript)
2829
* [Infer `distinct()` return types from schema](#infer-distinct-return-types-from-schema)
2930

3031
<h2 id="removed-rawresult-option-for-findoneandupdate"><a href="#removed-rawresult-option-for-findoneandupdate">Removed <code>rawResult</code> option for <code>findOneAndUpdate()</code></a></h2>
@@ -247,6 +248,39 @@ const doc = new TestModel();
247248
doc.name;
248249
```
249250

251+
<h2 id="model-constructor-properties-are-all-optional-in-typescript"><a href="#model-constructor-properties-are-all-optional-in-typescript">Model constructor properties are all optional in TypeScript</a></h2>
252+
253+
In Mongoose 8, no properties are required on model constructors by default.
254+
255+
```ts
256+
import {Schema, model, Model} from 'mongoose';
257+
258+
interface IDocument {
259+
name: string;
260+
createdAt: Date;
261+
updatedAt: Date;
262+
}
263+
264+
const documentSchema = new Schema<IDocument>(
265+
{ name: { type: String, required: true } },
266+
{ timestamps: true }
267+
);
268+
269+
const TestModel = model<IDocument>('Document', documentSchema);
270+
271+
// Would throw a compile error in Mongoose 7, compiles in Mongoose 8
272+
const newDoc = new TestModel({
273+
name: 'Foo'
274+
});
275+
276+
// Explicitly pass generic param to constructor to specify the expected
277+
// type of the model constructor param. The following will cause TS
278+
// to complain about missing `createdAt` and `updatedAt` in Mongoose 8.
279+
const newDoc2 = new TestModel<IDocument>({
280+
name: 'Foo'
281+
});
282+
```
283+
250284
<h2 id="infer-distinct-return-types-from-schema"><a href="#infer-distinct-return-types-from-schema">Infer <code>distinct()</code> return types from schema</a></h2>
251285

252286
```ts

0 commit comments

Comments
 (0)