From e6bab316d19e450d59e68a18ad9bbe272fc7f520 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Mon, 16 Nov 2020 17:45:27 -0500 Subject: [PATCH] chore(index.d.ts): exclude methods defined in interface from lean documents Address some feedback from @taxilian Re: #8108 --- index.d.ts | 5 ++++- test/typescript/leanDocuments.ts | 7 +++++++ test/typescript/main.test.js | 3 ++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index 0593c30015d..3184e817904 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1704,7 +1704,10 @@ declare module "mongoose" { export type DocumentDefinition = Omit>; - export type LeanDocument = Omit>; + type FunctionPropertyNames = { + [K in keyof T]: T[K] extends Function ? K : never + }[keyof T]; + export type LeanDocument = Omit>, FunctionPropertyNames>; class QueryCursor extends stream.Readable { /** diff --git a/test/typescript/leanDocuments.ts b/test/typescript/leanDocuments.ts index 0f72217e1a9..6c6dd5b2859 100644 --- a/test/typescript/leanDocuments.ts +++ b/test/typescript/leanDocuments.ts @@ -5,19 +5,26 @@ const schema: Schema = new Schema({ name: { type: 'String' } }); interface ITest extends Document { _id?: Types.ObjectId, name?: string; + testMethod: () => number; } +schema.method('testMethod', () => 42); + const Test = model('Test', schema); void async function main() { const doc: ITest = await Test.findOne(); + doc.testMethod(); + const pojo = doc.toObject(); await pojo.save(); const _doc = await Test.findOne().lean(); await _doc.save(); + _doc.testMethod(); + const hydrated = Test.hydrate(_doc); await hydrated.save(); }(); \ No newline at end of file diff --git a/test/typescript/main.test.js b/test/typescript/main.test.js index b9bb974f77a..d78d07eefb6 100644 --- a/test/typescript/main.test.js +++ b/test/typescript/main.test.js @@ -125,9 +125,10 @@ describe('typescript syntax', function() { if (process.env.D && errors.length) { console.log(errors); } - assert.equal(errors.length, 2); + assert.equal(errors.length, 3); assert.ok(errors[0].messageText.includes('Property \'save\' does not exist'), errors[0].messageText); assert.ok(errors[1].messageText.includes('Property \'save\' does not exist'), errors[0].messageText); + assert.ok(errors[2].messageText.includes('Property \'testMethod\' does not exist'), errors[0].messageText); }); });