Skip to content

Type 'Doc' does not satisfy the constraint 'Document<any, any, any>' when using discriminators #366

@Oluwatemilorun

Description

@Oluwatemilorun

After upgrading to v9.5.2 using composeWithMongooseDiscriminators throws a typescript error when casting Model types.

// 1. Create an interface representing a document in MongoDB.
export interface User {
	name: string;
	email: string;
	phone: string;
	type: UserType;
}

// 2. Create a Schema corresponding to the document interface.
export const UserSchema = new Schema<User>(
	{
		name: { type: String, required: true, maxlength: 50 },
		email: { type: String, unique: true, required: true },
		phone: { type: String, unique: true, required: true },

		type: {
			type: String,
			enum: Object.keys(UserType),
			default: UserType.CLIENT,
			required: true,
		},
	},
	{
		timestamps: true,
	}
);

// 3. set discriminator Key
UserSchema.set('discriminatorKey', 'type');

// 4. Create base model
const User = model<User>('User', UserSchema);

// 5. Create DiscriminatorTypeComposer
const UserDTC = composeWithMongooseDiscriminators<User, AppContext>(User, {});

The above example will throw an error

Type 'User' does not satisfy the constraint 'Document<any, any, any>'.
  Type 'User' is missing the following properties from type 'Document<any, any, any>': $getAllSubdocs, $ignore, $isDefault, $isDeleted, and 48 more.

graphql-compose-mongoose: ^9.5.2
mongoose: ^5.13.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions