Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): correct handling for model<any> #12659

Merged
merged 2 commits into from Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions test/types/models.test.ts
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);
vkarpov15 marked this conversation as resolved.
Show resolved Hide resolved
}
1 change: 0 additions & 1 deletion types/index.d.ts
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
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
@@ -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