Skip to content

Commit

Permalink
fix(types): correct handling for model<any>
Browse files Browse the repository at this point in the history
Fix #12573
  • Loading branch information
vkarpov15 committed Nov 4, 2022
1 parent ae14d0e commit a061ccd
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 2 deletions.
6 changes: 6 additions & 0 deletions test/types/models.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -561,3 +561,9 @@ function findWithId() {
TestModel.find(id);
TestModel.findOne(id);
}

function gh12573ModelAny() {
const TestModel = model<any>('Test', new Schema({}));
const doc = new TestModel();
expectType<any>(doc);
}
1 change: 0 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,6 @@ declare module 'mongoose' {
export type SchemaDefinitionType<T> = T extends Document ? Omit<T, Exclude<keyof Document, '_id' | 'id' | '__v'>> : T;

// Helpers to simplify checks
type IfAny<IFTYPE, THENTYPE, ELSETYPE = IFTYPE> = 0 extends (1 & IFTYPE) ? THENTYPE : ELSETYPE;
type IfUnknown<IFTYPE, THENTYPE> = unknown extends IFTYPE ? THENTYPE : IFTYPE;

// tests for these two types are located in test/types/lean.test.ts
Expand Down
2 changes: 1 addition & 1 deletion types/inferschematype.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ declare module 'mongoose' {
* // result
* type UserType = {userName?: string}
*/
type InferSchemaType<TSchema> = ObtainSchemaGeneric<TSchema, 'DocType'>;
type InferSchemaType<TSchema> = IfAny<TSchema, any, ObtainSchemaGeneric<TSchema, 'DocType'>>;

/**
* @summary Obtains schema Generic type by using generic alias.
Expand Down
1 change: 1 addition & 0 deletions types/utility.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
declare module 'mongoose' {
type IfAny<IFTYPE, THENTYPE, ELSETYPE = IFTYPE> = 0 extends (1 & IFTYPE) ? THENTYPE : ELSETYPE;

type Unpacked<T> = T extends (infer U)[] ?
U :
Expand Down

0 comments on commit a061ccd

Please sign in to comment.