From 9ba3f1289e1d16b5c661a786cb85f5aa6caef797 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Fri, 1 Jul 2022 13:43:28 -0400 Subject: [PATCH] fix(types): avoid inferring doc type from param to `create()` Fix #12001 Re: #11960 Re: #11563 --- test/types/create.test.ts | 8 ++++---- test/types/models.test.ts | 1 - types/models.d.ts | 10 +++++----- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/test/types/create.test.ts b/test/types/create.test.ts index debca167aeb..033010ae9bb 100644 --- a/test/types/create.test.ts +++ b/test/types/create.test.ts @@ -5,7 +5,7 @@ const schema: Schema = new Schema({ name: { type: 'String' } }); interface ITest { _id?: Types.ObjectId; - name?: string; + name: string; } const Test = model('Test', schema); @@ -105,7 +105,7 @@ Test.insertMany({ _id: '000000000000000000000000', name: 'test' }, (err, docs) = Test.insertMany({ _id: new Types.ObjectId('000000000000000000000000') }, (err, docs) => { expectType(docs[0]._id); - expectType(docs[0].name); + expectType(docs[0].name); expectType(docs[0].isNew); }); @@ -153,13 +153,13 @@ Test.insertMany([{ name: 'test' }], { lean: true }).then(docs => { Test.insertMany([{ name: 'test' }], { lean: false }).then(docs => { expectType(docs[0]._id); - expectType(docs[0].name); + expectType(docs[0].name); expectType(docs[0].isNew); }); Test.insertMany([{ name: 'test' }], { }).then(docs => { expectType(docs[0]._id); - expectType(docs[0].name); + expectType(docs[0].name); expectType(docs[0].isNew); }); diff --git a/test/types/models.test.ts b/test/types/models.test.ts index a1f52e06027..58941ae10aa 100644 --- a/test/types/models.test.ts +++ b/test/types/models.test.ts @@ -281,7 +281,6 @@ export function autoTypedModel() { // Model-functions-test // Create should works with arbitrary objects. const randomObject = await AutoTypedModel.create({ unExistKey: 'unExistKey', description: 'st' }); - expectType(randomObject.unExistKey); expectType(randomObject.userName); const testDoc1 = await AutoTypedModel.create({ userName: 'M0_0a' }); diff --git a/types/models.d.ts b/types/models.d.ts index b7877297d96..446a5c14da7 100644 --- a/types/models.d.ts +++ b/types/models.d.ts @@ -163,11 +163,11 @@ declare module 'mongoose' { countDocuments(callback?: Callback): QueryWithHelpers, TQueryHelpers, T>; /** Creates a new document or documents */ - create(docs: Array, options?: SaveOptions): Promise, RequireOnlyTypedId>, TMethodsAndOverrides, TVirtuals>[]>; - create(docs: Array, callback: Callback, RequireOnlyTypedId>, TMethodsAndOverrides, TVirtuals>[]>): void; - create(doc: DocContents | T): Promise, RequireOnlyTypedId>, TMethodsAndOverrides, TVirtuals>>; - create(...docs: Array): Promise, RequireOnlyTypedId>, TMethodsAndOverrides, TVirtuals>[]>; - create(doc: T | DocContents, callback: Callback, RequireOnlyTypedId>, TMethodsAndOverrides, TVirtuals>>): void; + create>(docs: Array, options?: SaveOptions): Promise[]>; + create>(docs: Array, callback: Callback[]>): void; + create>(doc: DocContents | T): Promise>; + create>(...docs: Array): Promise[]>; + create>(doc: T | DocContents, callback: Callback>): void; /** * Create the collection for this model. By default, if no indexes are specified,