Skip to content

Commit 5cbe563

Browse files
committed
docs(migrating_to_8): add note about null for optional keys in TypeScript
1 parent 905b28b commit 5cbe563

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

docs/migrating_to_8.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ If you're still on Mongoose 6.x or earlier, please read the [Mongoose 6.x to 7.x
1616
* [MongoDB Node Driver 6.0](#mongodb-node-driver-6)
1717
* [Removed `findOneAndRemove()`](#removed-findoneandremove)
1818
* [Removed id Setter](#removed-id-setter)
19+
* [Allow `null` For Optional Fields in TypeScript](#allow-null-for-optional-fields-in-typescript)
1920

2021
<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>
2122

@@ -59,4 +60,20 @@ Use `findOneAndDelete()` instead.
5960
<h2 id="removed-id-setter"><a href="#removed-id-setter">Removed id Setter</a></h2>
6061

6162
In Mongoose 7.4, Mongoose introduced an `id` setter that made `doc.id = '0'.repeat(24)` equivalent to `doc._id = '0'.repeat(24)`.
62-
In Mongoose 8, that setter is now removed.
63+
In Mongoose 8, that setter is now removed.
64+
65+
<h2 id="allow-null-for-optional-fields-in-typescript"><a href="#allow-null-for-optional-fields-in-typescript">Allow <code>null</code> For Optional Fields in TypeScript</a></h2>
66+
67+
In Mongoose 8, automatically inferred schema types in TypeScript allow `null` for optional fields.
68+
In Mongoose 7, optional fields only allowed `undefined`, not `null`.
69+
70+
```typescript
71+
const schema = new Schema({ name: String });
72+
const TestModel = model('Test', schema);
73+
74+
const doc = new TestModel();
75+
76+
// In Mongoose 8, this type is `string | null | undefined`.
77+
// In Mongoose 7, this type is `string | undefined`
78+
doc.name;
79+
```

0 commit comments

Comments
 (0)