Skip to content

Commit d318339

Browse files
committedDec 10, 2020
fix(index.d.ts): make Document#id optional so types that use id can use Model<IMyType & Document>
Fix #9684
1 parent a9b317a commit d318339

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed
 

‎index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ declare module "mongoose" {
475475
getChanges(): UpdateQuery<this>;
476476

477477
/** The string version of this documents _id. */
478-
id: string;
478+
id?: string;
479479

480480
/** Signal that we desire an increment of this documents version. */
481481
increment(): this;

‎test/typescript/models.ts

+27-11
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,37 @@
11
import { Schema, Document, Model, connection } from 'mongoose';
22

3-
interface ITest extends Document {
4-
foo: string;
3+
function conventionalSyntax(): void {
4+
interface ITest extends Document {
5+
foo: string;
6+
}
7+
8+
const TestSchema = new Schema({
9+
foo: { type: String, required: true },
10+
});
11+
12+
const Test = connection.model<ITest>('Test', TestSchema);
13+
14+
const bar = (SomeModel: Model<ITest>) => console.log(SomeModel);
15+
16+
bar(Test);
517
}
618

7-
const TestSchema = new Schema({
8-
foo: { type: String, required: true },
9-
});
19+
function tAndDocSyntax(): void {
20+
interface ITest {
21+
id: number;
22+
foo: string;
23+
}
24+
25+
const TestSchema = new Schema({
26+
foo: { type: String, required: true },
27+
});
1028

11-
const Test = connection.model<ITest>('Test', TestSchema);
29+
const Test = connection.model<ITest & Document>('Test', TestSchema);
1230

13-
const bar = (SomeModel: Model<ITest>) => // <<<< error here
14-
console.log(SomeModel);
31+
const aggregated: Promise<Document> = Test.aggregate([]).then(res => res[0]);
1532

16-
bar(Test);
33+
const bar = (SomeModel: Model<ITest & Document>) => console.log(SomeModel);
34+
}
1735

1836
const ExpiresSchema = new Schema({
1937
ttl: {
@@ -22,8 +40,6 @@ const ExpiresSchema = new Schema({
2240
},
2341
});
2442

25-
const aggregated: Promise<Document> = Test.aggregate([]).then(res => res[0]);
26-
2743
interface IProject extends Document {
2844
name: String;
2945
}

0 commit comments

Comments
 (0)
Please sign in to comment.