Skip to content

Commit c802909

Browse files
committed
test(types): add test case for #14573
1 parent 1e652c6 commit c802909

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

test/types/schema.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import {
2+
DefaultSchemaOptions,
3+
HydratedSingleSubdocument,
24
Schema,
35
Document,
46
HydratedDocument,
@@ -1442,3 +1444,53 @@ function gh14367() {
14421444
flags: [true]
14431445
};
14441446
}
1447+
1448+
function gh14573() {
1449+
interface Names {
1450+
_id: Types.ObjectId;
1451+
firstName: string;
1452+
}
1453+
1454+
// Document definition
1455+
interface User {
1456+
names: Names;
1457+
}
1458+
1459+
// Define property overrides for hydrated documents
1460+
type THydratedUserDocument = {
1461+
names?: HydratedSingleSubdocument<Names>;
1462+
};
1463+
1464+
type UserMethods = {
1465+
getName(): Names | undefined;
1466+
};
1467+
1468+
type UserModelType = Model<User, {}, UserMethods, {}, THydratedUserDocument>;
1469+
1470+
const userSchema = new Schema<
1471+
User,
1472+
UserModelType,
1473+
UserMethods,
1474+
{},
1475+
{},
1476+
{},
1477+
DefaultSchemaOptions,
1478+
User,
1479+
THydratedUserDocument
1480+
>(
1481+
{
1482+
names: new Schema<Names>({ firstName: String })
1483+
},
1484+
{
1485+
methods: {
1486+
getName() {
1487+
const str: string | undefined = this.names?.firstName;
1488+
return this.names?.toObject();
1489+
}
1490+
}
1491+
}
1492+
);
1493+
const UserModel = model<User, UserModelType>('User', userSchema);
1494+
const doc = new UserModel({ names: { _id: '0'.repeat(24), firstName: 'foo' } });
1495+
doc.names?.ownerDocument();
1496+
}

0 commit comments

Comments
 (0)