'Type of property 'signup' circularly references itself in mapped type...' error when mongoose static uses "this..."

9 hours ago 3
ARTICLE AD BOX

This issue comes from how TypeScript infers the type of statics in Mongoose when you mix:

Schema<T, ModelType> static methods using this object-literal inference on schema.statics

TypeScript tries to infer the entire statics object as a single contextual type. When one or more statics use this, it causes the model type (IUserModel) to recursively reference itself through Model, which can lead to inference issues or errors like circular references or incorrect this typing.

This part is important:

interface IUserModel extends Model<IUser>

This is only the model type, not the schema type. But when used as:

new Schema<IUser, IUserModel>

TypeScript attempts to unify:

document shape (IUser) model shape (IUserModel) statics (schema.statics)

When statics use this, inference becomes circular.

The fix is to explicitly type this inside each static method, so TypeScript does not try to infer it from the schema context.

import { Schema, Model } from "mongoose"; interface IUser { email: string; username: string; } interface IUserModel extends Model<IUser> { findByEmail(email: string): Promise<IUser | null>; findByUsername(username: string): Promise<IUser | null>; } const userSchema = new Schema<IUser, IUserModel>({ email: String, username: String, }); userSchema.statics.findByEmail = async function ( this: IUserModel, email: string ) { return this.findOne({ email }); }; userSchema.statics.findByUsername = async function ( this: IUserModel, username: string ) { return this.findOne({ username }); };
Read Entire Article