[Types] DocumentArray.caster
should be Types.Subdocument
, not typeof Types.Subdocument
#15179
Open
Description
Prerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the bug has not already been reported
Mongoose version
8.9.3
Node.js version
20.15.1
MongoDB server version
8
Typescript version (if applicable)
5.6.3
Description
In Schema.Types.Array
, the caster
is an instance of SchemaType
. It would make sense that for other SchemaTypes, the caster would be an instance of a subclass of SchemaType
.
However, in DocumentArray
the caster
is typeof Types.Subdocument
, which is a function, not an instance of SchemaType itself. Instead, caster
should just be Types.Subdocument
.
https://github.com/Automattic/mongoose/blob/master/types/schematypes.d.ts#L428
class DocumentArray extends SchemaType implements AcceptsDiscriminator {
/** This schema type's name, to defend against minifiers that mangle function names. */
static schemaName: 'DocumentArray';
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;
/** The schema used for documents in this array */
schema: Schema;
/** The constructor used for subdocuments in this array */
- caster?: typeof Types.Subdocument;
+ caster?: Types.Subdocument;
/** Default options for this SchemaType */
defaultOptions: Record<string, any>;
}
Steps to Reproduce
Here's an example of how the typing causes issues:
function isStringArray(schemaType: Schema.Types.Array | Schema.Types.DocumentArray): boolean {
return isStringType(schemaType.caster);
// SchemaType | typeof Types.Subdocument | undefined is not assignable to SchemaType | undefined
}
function isStringType(schemaType?: SchemaType): boolean {
if (!schemaType) return false;
return schemaType instanceof Schema.Types.String;
}
Expected Behavior
No response