Skip to content

Commit

Permalink
chore(index.d.ts): pull any types into LeanDocuments correctly
Browse files Browse the repository at this point in the history
Re: #8108
  • Loading branch information
vkarpov15 committed Nov 17, 2020
1 parent e6bab31 commit 16be7cb
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
5 changes: 3 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1704,8 +1704,9 @@ declare module "mongoose" {

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

type FunctionPropertyNames<T> = {
[K in keyof T]: T[K] extends Function ? K : never
type FunctionPropertyNames<T> = {
// The 1 & T[K] check comes from: https://stackoverflow.com/questions/55541275/typescript-check-for-the-any-type
[K in keyof T]: 0 extends (1 & T[K]) ? never : (T[K] extends Function ? K : never)
}[keyof T];
export type LeanDocument<T> = Omit<Omit<T, Exclude<keyof Document, '_id'>>, FunctionPropertyNames<T>>;

Expand Down
3 changes: 3 additions & 0 deletions test/typescript/leanDocuments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const schema: Schema = new Schema({ name: { type: 'String' } });
interface ITest extends Document {
_id?: Types.ObjectId,
name?: string;
mixed?: any;
testMethod: () => number;
}

Expand All @@ -24,6 +25,8 @@ void async function main() {
await _doc.save();

_doc.testMethod();
_doc.name = 'test';
_doc.mixed = 42;

const hydrated = Test.hydrate(_doc);
await hydrated.save();
Expand Down

0 comments on commit 16be7cb

Please sign in to comment.