Skip to content

Commit

Permalink
chore(index.d.ts): exclude methods defined in interface from lean doc…
Browse files Browse the repository at this point in the history
…uments

Address some feedback from @taxilian

Re: #8108
  • Loading branch information
vkarpov15 committed Nov 16, 2020
1 parent 5bd9b2b commit e6bab31
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
5 changes: 4 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1704,7 +1704,10 @@ declare module "mongoose" {

export type DocumentDefinition<T> = Omit<T, Exclude<keyof Document, '_id'>>;

export type LeanDocument<T> = Omit<T, Exclude<keyof Document, '_id'>>;
type FunctionPropertyNames<T> = {
[K in keyof T]: T[K] extends Function ? K : never
}[keyof T];
export type LeanDocument<T> = Omit<Omit<T, Exclude<keyof Document, '_id'>>, FunctionPropertyNames<T>>;

class QueryCursor<DocType extends Document> extends stream.Readable {
/**
Expand Down
7 changes: 7 additions & 0 deletions test/typescript/leanDocuments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ITest>('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();
}();
3 changes: 2 additions & 1 deletion test/typescript/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

Expand Down

0 comments on commit e6bab31

Please sign in to comment.