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

Model's type parameter must be defined #12212

Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion test/types/document.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ function gh11435() {
const ItemSchema = new Schema<Item>({ name: String });

ItemSchema.pre('validate', function preValidate() {
expectType<Model<unknown>>(this.$model('Item1'));
expectType<Model<{}>>(this.$model('Item1'));
});
}

Expand Down
4 changes: 2 additions & 2 deletions types/connection.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,13 @@ declare module 'mongoose' {
readonly models: Readonly<{ [index: string]: Model<any> }>;

/** Defines or retrieves a model. */
model<T, U, TQueryHelpers = {}>(
model<T extends {}, U, TQueryHelpers = {}>(
name: string,
schema?: Schema<T, any, any, TQueryHelpers, any, any>,
collection?: string,
options?: CompileModelOptions
): U;
model<T>(name: string, schema?: Schema<T>, collection?: string, options?: CompileModelOptions): Model<T>;
model<T extends {}>(name: string, schema?: Schema<T>, collection?: string, options?: CompileModelOptions): Model<T>;

/** Returns an array of model names created on this connection. */
modelNames(): Array<string>;
Expand Down
2 changes: 1 addition & 1 deletion types/document.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ declare module 'mongoose' {
$markValid(path: string): void;

/** Returns the model with the given name on this document's associated connection. */
$model<ModelType = Model<unknown>>(name: string): ModelType;
$model<ModelType = Model<{}>>(name: string): ModelType;

/**
* A string containing the current operation that Mongoose is executing
Expand Down
10 changes: 5 additions & 5 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ declare module 'mongoose' {
options?: CompileModelOptions
): Model<InferSchemaType<TSchema>, ObtainSchemaGeneric<TSchema, 'TQueryHelpers'>, ObtainSchemaGeneric<TSchema, 'TInstanceMethods'>, {}, TSchema> & ObtainSchemaGeneric<TSchema, 'TStaticMethods'>;

export function model<T>(name: string, schema?: Schema<T, any, any> | Schema<T & Document, any, any>, collection?: string, options?: CompileModelOptions): Model<T>;
export function model<T extends {}>(name: string, schema?: Schema<T, any, any> | Schema<T & Document, any, any>, collection?: string, options?: CompileModelOptions): Model<T>;

export function model<T, U, TQueryHelpers = {}>(
export function model<T extends {}, U, TQueryHelpers = {}>(
name: string,
schema?: Schema<T, any, any, TQueryHelpers, any, any>,
collection?: string,
Expand Down Expand Up @@ -151,15 +151,15 @@ declare module 'mongoose' {
: M
: M;

export type DiscriminatorSchema<DocType, M, TInstanceMethods, TQueryHelpers, TVirtuals, T> = T extends Schema<infer T1, infer T2, infer T3, infer T4, infer T5>
export type DiscriminatorSchema<DocType extends {}, M, TInstanceMethods, TQueryHelpers, TVirtuals, T> = T extends Schema<infer T1, infer T2, infer T3, infer T4, infer T5>
? Schema<Omit<DocType, keyof T1> & T1, DiscriminatorModel<T2, M>, T3 | TInstanceMethods, T4 | TQueryHelpers, T5 | TVirtuals>
: Schema<DocType, M, TInstanceMethods, TQueryHelpers, TVirtuals>;

type QueryResultType<T> = T extends Query<infer ResultType, any> ? ResultType : never;

type PluginFunction<DocType> = (schema: Schema<DocType>, opts?: any) => void;
type PluginFunction<DocType extends {}> = (schema: Schema<DocType>, opts?: any) => void;

export class Schema<EnforcedDocType = any, M = Model<EnforcedDocType, any, any, any>, TInstanceMethods = {}, TQueryHelpers = {}, TVirtuals = {},
export class Schema<EnforcedDocType extends {} = any, M = Model<EnforcedDocType, any, any, any>, TInstanceMethods = {}, TQueryHelpers = {}, TVirtuals = {},
TStaticMethods = {},
TPathTypeKey extends TypeKeyBaseType = DefaultTypeKey,
DocType extends ObtainDocumentType<DocType, EnforcedDocType, TPathTypeKey> = ObtainDocumentType<any, EnforcedDocType, TPathTypeKey>>
Expand Down
6 changes: 3 additions & 3 deletions types/models.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ declare module 'mongoose' {

export interface AcceptsDiscriminator {
/** Adds a discriminator type. */
discriminator<D>(name: string | number, schema: Schema, value?: string | number | ObjectId): Model<D>;
discriminator<T, U>(name: string | number, schema: Schema<T, U>, value?: string | number | ObjectId): U;
discriminator<D extends {}>(name: string | number, schema: Schema, value?: string | number | ObjectId): Model<D>;
discriminator<T extends {}, U>(name: string | number, schema: Schema<T, U>, value?: string | number | ObjectId): U;
}

interface MongooseBulkWriteOptions {
Expand Down Expand Up @@ -114,7 +114,7 @@ declare module 'mongoose' {
}

const Model: Model<any>;
interface Model<T, TQueryHelpers = {}, TMethodsAndOverrides = {}, TVirtuals = {}, TSchema = any> extends
interface Model<T extends {}, TQueryHelpers = {}, TMethodsAndOverrides = {}, TVirtuals = {}, TSchema = any> extends
NodeJS.EventEmitter,
AcceptsDiscriminator,
IndexManager,
Expand Down
2 changes: 1 addition & 1 deletion types/schemaoptions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ declare module 'mongoose' {
type TypeKeyBaseType = string;

type DefaultTypeKey = 'type';
interface SchemaOptions<PathTypeKey extends TypeKeyBaseType = DefaultTypeKey, DocType = unknown, TInstanceMethods = {}, QueryHelpers = {}, TStaticMethods = {}, TVirtuals = {}> {
interface SchemaOptions<PathTypeKey extends TypeKeyBaseType = DefaultTypeKey, DocType extends {} = {}, TInstanceMethods = {}, QueryHelpers = {}, TStaticMethods = {}, TVirtuals = {}> {
/**
* By default, Mongoose's init() function creates all the indexes defined in your model's schema by
* calling Model.createIndexes() after you successfully connect to MongoDB. If you want to disable
Expand Down
12 changes: 6 additions & 6 deletions types/schematypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ declare module 'mongoose' {

static options: { castNonArrays: boolean; };

discriminator<T, U>(name: string | number, schema: Schema<T, U>, value?: string): U;
discriminator<D>(name: string | number, schema: Schema, value?: string): Model<D>;
discriminator<T extends {}, U>(name: string | number, schema: Schema<T, U>, value?: string): U;
discriminator<D extends {}>(name: string | number, schema: Schema, value?: string): Model<D>;

/** The schematype embedded in this array */
caster?: SchemaType;
Expand Down Expand Up @@ -344,8 +344,8 @@ declare module 'mongoose' {

static options: { castNonArrays: boolean; };

discriminator<D>(name: string | number, schema: Schema, value?: string): Model<D>;
discriminator<T, U>(name: string | number, schema: Schema<T, U>, value?: string): U;
discriminator<D extends {}>(name: string | number, schema: Schema, value?: string): Model<D>;
discriminator<T extends {}, U>(name: string | number, schema: Schema<T, U>, value?: string): U;

/** The schema used for documents in this array */
schema: Schema;
Expand Down Expand Up @@ -393,8 +393,8 @@ declare module 'mongoose' {
/** The document's schema */
schema: Schema;

discriminator<T, U>(name: string | number, schema: Schema<T, U>, value?: string): U;
discriminator<D>(name: string | number, schema: Schema, value?: string): Model<D>;
discriminator<T extends {}, U>(name: string | number, schema: Schema<T, U>, value?: string): U;
discriminator<D extends {}>(name: string | number, schema: Schema, value?: string): Model<D>;
}

class String extends SchemaType {
Expand Down