Skip to content

Commit 4a2cb0b

Browse files
committed
types(models): make all properties to Model constructor optional
Fix #13897 Re: #11563
1 parent 8831f03 commit 4a2cb0b

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

test/types/models.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,3 +751,22 @@ function gh13957() {
751751
const repository = new RepositoryBase<ITest>(TestModel);
752752
expectType<Promise<ITest[]>>(repository.insertMany([{ name: 'test' }]));
753753
}
754+
755+
function gh13897() {
756+
interface IDocument {
757+
name: string;
758+
createdAt: Date;
759+
updatedAt: Date;
760+
}
761+
762+
const documentSchema = new Schema<IDocument>({
763+
name: { type: String, required: true }
764+
},
765+
{
766+
timestamps: true
767+
});
768+
769+
const Document = model<IDocument>('Document', documentSchema);
770+
const doc = new Document({ name: 'foo' });
771+
expectType<Date>(doc.createdAt);
772+
}

types/models.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ declare module 'mongoose' {
158158
AcceptsDiscriminator,
159159
IndexManager,
160160
SessionStarter {
161-
new <DocType = TRawDocType>(doc?: DocType, fields?: any | null, options?: boolean | AnyObject): THydratedDocumentType;
161+
new <DocType = Partial<TRawDocType>>(doc?: DocType, fields?: any | null, options?: boolean | AnyObject): THydratedDocumentType;
162162

163163
aggregate<R = any>(pipeline?: PipelineStage[], options?: AggregateOptions): Aggregate<Array<R>>;
164164
aggregate<R = any>(pipeline: PipelineStage[]): Aggregate<Array<R>>;

0 commit comments

Comments
 (0)