Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions test/types/document.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,3 +475,47 @@ async function gh15316() {
expectType<string>(doc.toJSON({ virtuals: true }).upper);
expectType<string>(doc.toObject({ virtuals: true }).upper);
}

function gh13079() {
const schema = new Schema({
name: { type: String, required: true }
});
const TestModel = model('Test', schema);

const doc = new TestModel({ name: 'taco' });
expectType<string>(doc.id);

const schema2 = new Schema({
id: { type: Number, required: true },
name: { type: String, required: true }
});
const TestModel2 = model('Test', schema2);

const doc2 = new TestModel2({ name: 'taco' });
expectType<number>(doc2.id);

const schema3 = new Schema<{ name: string }>({
name: { type: String, required: true }
});
const TestModel3 = model('Test', schema3);

const doc3 = new TestModel3({ name: 'taco' });
expectType<string>(doc3.id);

const schema4 = new Schema<{ name: string, id: number }>({
id: { type: Number, required: true },
name: { type: String, required: true }
});
const TestModel4 = model('Test', schema4);

const doc4 = new TestModel4({ name: 'taco' });
expectType<number>(doc4.id);

const schema5 = new Schema({
name: { type: String, required: true }
}, { id: false });
const TestModel5 = model('Test', schema5);

const doc5 = new TestModel5({ name: 'taco' });
expectError(doc5.id);
}
2 changes: 1 addition & 1 deletion test/types/virtuals.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function gh11543() {

async function autoTypedVirtuals() {
type AutoTypedSchemaType = InferSchemaType<typeof testSchema>;
type VirtualsType = { domain: string };
type VirtualsType = { domain: string } & { id: string };
type InferredDocType = AutoTypedSchemaType & ObtainSchemaGeneric<typeof testSchema, 'TVirtuals'>;

const testSchema = new Schema({
Expand Down
3 changes: 0 additions & 3 deletions types/document.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,6 @@ declare module 'mongoose' {
*/
getChanges(): UpdateQuery<this>;

/** The string version of this documents _id. */
id?: any;

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

Expand Down
2 changes: 1 addition & 1 deletion types/inferschematype.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ declare module 'mongoose' {
M: M;
TInstanceMethods: TInstanceMethods;
TQueryHelpers: TQueryHelpers;
TVirtuals: TVirtuals;
TVirtuals: (DocType extends { id: any } ? TVirtuals : TSchemaOptions extends { id: false } ? TVirtuals : TVirtuals & { id: string });
TStaticMethods: TStaticMethods;
TSchemaOptions: TSchemaOptions;
DocType: DocType;
Expand Down
Loading