-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
[Types] DocumentArray.caster
should be Types.Subdocument
, not typeof Types.Subdocument
#15179
Comments
It looks like the typings are correct, consider the following script: 'use strict';
const mongoose = require('mongoose');
const { Schema } = mongoose;
const schema = new Schema({ docArray: [new Schema({ name: String })] });
console.log(schema.path('docArray').caster); Output is:
So yes, Consider using |
The array of strings example is contrived. Also, I don't think that console output demonstrates that await mongoose.connect('mongodb://localhost:27017/mongoose_test');
const schema = new mongoose.Schema({
docArray: {
type: [
new mongoose.Schema({ test: String })
]
}
});
const docArray = schema.path('docArray');
console.log('is DocumentArray', docArray instanceof mongoose.Schema.Types.DocumentArray);
console.log('is Array', docArray instanceof mongoose.Schema.Types.Array); // DocumentArrays are instances of Array too
console.log(docArray.caster.schema); // If `caster` is a function, this should not be possible That gives the following output: 'is DocumentArray' true
'is Array' true
{ obj: { test: [Function: String] },
paths:
{ test: ...,
_id: ... },
aliases: {},
subpaths: {},
virtuals: ...,
singleNestedPaths: {},
nested: {},
inherits: {},
callQueue: [],
_indexes: [],
methods: {},
methodOptions: {},
statics: {},
tree: ...,
query: {},
childSchemas: [],
plugins: [],
'$id': 6,
mapPaths: [],
s: ...,
_userProvidedOptions: {},
options: ... } If if (docArray instanceof Schema.Types.DocumentArray) {
console.log(schemaType.caster.schema);
// ERROR: Property 'schema' does not exist on type 'typeof Subdocument'.ts(2339)
} Also, this example appears to show that |
"If caster was actually a function, you would not be able to access members like a true class instantiation." <-- this doesn't seem to be true. Functions are objects in JavaScript, so you can assign properties to functions: "Also, this example appears to show that DocumentArray is a subclass of Array. Should not their caster fields be compatible?" DocumentArray is a subclass of Array, that is true. We will consider changing this in our next major release. In JavaScript, there's no such restriction that DocumentArray's |
This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days |
Prerequisites
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
, thecaster
is an instance ofSchemaType
. It would make sense that for other SchemaTypes, the caster would be an instance of a subclass ofSchemaType
.However, in
DocumentArray
thecaster
istypeof Types.Subdocument
, which is a function, not an instance of SchemaType itself. Instead,caster
should just beTypes.Subdocument
.https://github.com/Automattic/mongoose/blob/master/types/schematypes.d.ts#L428
Steps to Reproduce
Here's an example of how the typing causes issues:
Expected Behavior
No response
The text was updated successfully, but these errors were encountered: