|
7 | 7 | HydratedDocument,
|
8 | 8 | HydratedArraySubdocument,
|
9 | 9 | HydratedSingleSubdocument,
|
10 |
| - DefaultSchemaOptions |
| 10 | + DefaultSchemaOptions, |
| 11 | + ObtainSchemaGeneric, |
| 12 | + ResolveSchemaOptions |
11 | 13 | } from 'mongoose';
|
12 | 14 | import { DeleteResult } from 'mongodb';
|
13 | 15 | import { expectAssignable, expectError, expectNotAssignable, expectType } from 'tsd';
|
@@ -475,3 +477,135 @@ async function gh15316() {
|
475 | 477 | expectType<string>(doc.toJSON({ virtuals: true }).upper);
|
476 | 478 | expectType<string>(doc.toObject({ virtuals: true }).upper);
|
477 | 479 | }
|
| 480 | + |
| 481 | +async function gh15578() { |
| 482 | + function withDocType() { |
| 483 | + interface RawDocType { |
| 484 | + _id: Types.ObjectId; |
| 485 | + testProperty: number; |
| 486 | + } |
| 487 | + |
| 488 | + const ASchema = new Schema<RawDocType>({ |
| 489 | + testProperty: Number |
| 490 | + }); |
| 491 | + |
| 492 | + const AModel = model<RawDocType>('YourModel', ASchema); |
| 493 | + |
| 494 | + const a = new AModel({ testProperty: 8 }); |
| 495 | + const toObjectFlattened: Omit<RawDocType, '_id'> & { _id: string } = a.toObject({ flattenObjectIds: true }); |
| 496 | + const toObjectWithVirtuals: Omit<RawDocType, '_id'> & { _id: string } = a.toObject({ virtuals: true, flattenObjectIds: true }); |
| 497 | + const toObjectWithoutVirtuals: Omit<RawDocType, '_id'> & { _id: string } = a.toObject({ virtuals: false, flattenObjectIds: true }); |
| 498 | + const toJSONFlattened: Omit<RawDocType, '_id'> & { _id: string } = a.toJSON({ flattenObjectIds: true }); |
| 499 | + const toJSONWithVirtuals: Omit<RawDocType, '_id'> & { _id: string } = a.toJSON({ virtuals: true, flattenObjectIds: true }); |
| 500 | + const toJSONWithoutVirtuals: Omit<RawDocType, '_id'> & { _id: string } = a.toJSON({ virtuals: false, flattenObjectIds: true }); |
| 501 | + |
| 502 | + const objWithoutVersionKey = a.toObject({ versionKey: false }); |
| 503 | + const jsonWithoutVersionKey = a.toJSON({ versionKey: false }); |
| 504 | + expectError(objWithoutVersionKey.__v); |
| 505 | + expectError(jsonWithoutVersionKey.__v); |
| 506 | + |
| 507 | + const objWithVersionKey = a.toObject(); |
| 508 | + const jsonWithVersionKey = a.toJSON(); |
| 509 | + expectType<number>(objWithVersionKey.__v); |
| 510 | + expectType<number>(jsonWithVersionKey.__v); |
| 511 | + } |
| 512 | + |
| 513 | + function withDocTypeAndVersionKey() { |
| 514 | + interface RawDocType { |
| 515 | + _id: Types.ObjectId; |
| 516 | + testProperty: number; |
| 517 | + } |
| 518 | + |
| 519 | + const schemaOptions = { versionKey: 'taco' } as const; |
| 520 | + |
| 521 | + type ModelType = Model<RawDocType, {}, {}, {}, HydratedDocument<RawDocType, {}, {}, {}, typeof schemaOptions>>; |
| 522 | + |
| 523 | + const ASchema = new Schema<RawDocType, ModelType, {}, {}, {}, {}, typeof schemaOptions>({ |
| 524 | + testProperty: Number |
| 525 | + }, schemaOptions); |
| 526 | + |
| 527 | + const AModel = model<RawDocType, ModelType>('YourModel', ASchema); |
| 528 | + |
| 529 | + const a = new AModel({ testProperty: 8 }); |
| 530 | + const toObjectFlattened: Omit<RawDocType, '_id'> & { _id: string } = a.toObject({ flattenObjectIds: true }); |
| 531 | + const toObjectWithVirtuals: Omit<RawDocType, '_id'> & { _id: string } = a.toObject({ virtuals: true, flattenObjectIds: true }); |
| 532 | + const toObjectWithoutVirtuals: Omit<RawDocType, '_id'> & { _id: string } = a.toObject({ virtuals: false, flattenObjectIds: true }); |
| 533 | + const toJSONFlattened: Omit<RawDocType, '_id'> & { _id: string } = a.toJSON({ flattenObjectIds: true }); |
| 534 | + const toJSONWithVirtuals: Omit<RawDocType, '_id'> & { _id: string } = a.toJSON({ virtuals: true, flattenObjectIds: true }); |
| 535 | + const toJSONWithoutVirtuals: Omit<RawDocType, '_id'> & { _id: string } = a.toJSON({ virtuals: false, flattenObjectIds: true }); |
| 536 | + |
| 537 | + const objWithoutVersionKey = a.toObject({ versionKey: false }); |
| 538 | + const jsonWithoutVersionKey = a.toJSON({ versionKey: false }); |
| 539 | + expectError(objWithoutVersionKey.taco); |
| 540 | + expectError(jsonWithoutVersionKey.taco); |
| 541 | + |
| 542 | + const objWithVersionKey = a.toObject(); |
| 543 | + const jsonWithVersionKey = a.toJSON(); |
| 544 | + expectType<number>(objWithVersionKey.taco); |
| 545 | + expectType<number>(jsonWithVersionKey.taco); |
| 546 | + } |
| 547 | + |
| 548 | + function autoInferred() { |
| 549 | + interface RawDocType { |
| 550 | + _id: Types.ObjectId; |
| 551 | + testProperty?: number | null; |
| 552 | + } |
| 553 | + |
| 554 | + const ASchema = new Schema({ |
| 555 | + testProperty: Number |
| 556 | + }); |
| 557 | + |
| 558 | + const AModel = model('YourModel', ASchema); |
| 559 | + |
| 560 | + const a = new AModel({ testProperty: 8 }); |
| 561 | + const toObjectFlattened: Omit<RawDocType, '_id'> & { _id: string } = a.toObject({ flattenObjectIds: true }); |
| 562 | + const toObjectWithVirtuals: Omit<RawDocType, '_id'> & { _id: string } = a.toObject({ virtuals: true, flattenObjectIds: true }); |
| 563 | + const toObjectWithoutVirtuals: Omit<RawDocType, '_id'> & { _id: string } = a.toObject({ virtuals: false, flattenObjectIds: true }); |
| 564 | + const toJSONFlattened: Omit<RawDocType, '_id'> & { _id: string } = a.toJSON({ flattenObjectIds: true }); |
| 565 | + const toJSONWithVirtuals: Omit<RawDocType, '_id'> & { _id: string } = a.toJSON({ virtuals: true, flattenObjectIds: true }); |
| 566 | + const toJSONWithoutVirtuals: Omit<RawDocType, '_id'> & { _id: string } = a.toJSON({ virtuals: false, flattenObjectIds: true }); |
| 567 | + |
| 568 | + const objWithoutVersionKey = a.toObject({ versionKey: false }); |
| 569 | + const jsonWithoutVersionKey = a.toJSON({ versionKey: false }); |
| 570 | + expectError(objWithoutVersionKey.__v); |
| 571 | + expectError(jsonWithoutVersionKey.__v); |
| 572 | + |
| 573 | + const objWithVersionKey = a.toObject(); |
| 574 | + const jsonWithVersionKey = a.toJSON(); |
| 575 | + expectType<number>(objWithVersionKey.__v); |
| 576 | + expectType<number>(jsonWithVersionKey.__v); |
| 577 | + } |
| 578 | + |
| 579 | + function autoInferredWithCustomVersionKey() { |
| 580 | + interface RawDocType { |
| 581 | + _id: Types.ObjectId; |
| 582 | + testProperty?: number | null; |
| 583 | + } |
| 584 | + |
| 585 | + const ASchema = new Schema({ |
| 586 | + testProperty: Number |
| 587 | + }, { versionKey: 'taco' }); |
| 588 | + |
| 589 | + const AModel = model('YourModel', ASchema); |
| 590 | + |
| 591 | + type TSchemaOptions = ResolveSchemaOptions<ObtainSchemaGeneric<typeof ASchema, 'TSchemaOptions'>>; |
| 592 | + |
| 593 | + const a = new AModel({ testProperty: 8 }); |
| 594 | + const toObjectFlattened: Omit<RawDocType, '_id'> & { _id: string } = a.toObject({ flattenObjectIds: true }); |
| 595 | + const toObjectWithVirtuals: Omit<RawDocType, '_id'> & { _id: string } = a.toObject({ virtuals: true, flattenObjectIds: true }); |
| 596 | + const toObjectWithoutVirtuals: Omit<RawDocType, '_id'> & { _id: string } = a.toObject({ virtuals: false, flattenObjectIds: true }); |
| 597 | + const toJSONFlattened: Omit<RawDocType, '_id'> & { _id: string } = a.toJSON({ flattenObjectIds: true }); |
| 598 | + const toJSONWithVirtuals: Omit<RawDocType, '_id'> & { _id: string } = a.toJSON({ virtuals: true, flattenObjectIds: true }); |
| 599 | + const toJSONWithoutVirtuals: Omit<RawDocType, '_id'> & { _id: string } = a.toJSON({ virtuals: false, flattenObjectIds: true }); |
| 600 | + |
| 601 | + const objWithoutVersionKey = a.toObject({ versionKey: false }); |
| 602 | + const jsonWithoutVersionKey = a.toJSON({ versionKey: false }); |
| 603 | + expectError(objWithoutVersionKey.taco); |
| 604 | + expectError(jsonWithoutVersionKey.taco); |
| 605 | + |
| 606 | + const objWithVersionKey = a.toObject(); |
| 607 | + const jsonWithVersionKey = a.toJSON(); |
| 608 | + expectType<number>(objWithVersionKey.taco); |
| 609 | + expectType<number>(jsonWithVersionKey.taco); |
| 610 | + } |
| 611 | +} |
0 commit comments