-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
InferSchemaType
returns unknown
value
#14367
Comments
You're right, this is due to the |
types: consistently infer array of objects in schema as a DocumentArray
the issue with nested type is fixed but now all the arrays are export const UserSchema = new Schema({
languages: [
{
name: SchemaTypes.String,
spoken: SchemaTypes.Boolean,
},
],
roles: [SchemaTypes.String],
});
export type IUser = InferSchemaType<typeof UserSchema>;
const a: IUser = {
languages: [
{
name: 'English',
spoken: true,
},
],
};
// Type '{ name: string; spoken: true; }' is not assignable to type 'Subdocument<ObjectId> & { name?: string | null | undefined; spoken?: boolean | null | undefined; }'.
// Type '{ name: string; spoken: true; }' is missing the following properties from type 'Subdocument<ObjectId>': $isSingleNested, ownerDocument, parent, $parent, and 50 more.ts(2322)
// types.d.ts(63, 11): The expected type comes from property '0' which is declared here on type 'DocumentArray<{ name?: string | null | undefined; spoken?: boolean | null | undefined; }>'
a.roles.includes('a');
// Argument of type 'string' is not assignable to parameter of type 'Subdocument<ObjectId> & { get?: {} | null | undefined; set?: {} | null | undefined; prototype?: string | null | undefined; schemaName?: string | null | undefined; cast?: {} | ... 1 more ... | undefined; checkRequired?: {} | ... 1 more ... | undefined; }'.
// Type 'string' is not assignable to type 'Subdocument<ObjectId>'.ts(2345) |
Hi @imranbarbhuiya |
We have a PR to fix For |
types(schema): correctly infer Array<Schema.Types.*>
Prerequisites
Mongoose version
>=8.0.2
Node.js version
20
MongoDB server version
Not needed
Typescript version (if applicable)
5.3.3
Description
I was updating Mongoose and found all the prop values of the schema types became
unknown
so I tried to check previous versions and found it's starting from8.0.2
, after checking commits and reverting them, and found this pr is causing the issue. It happens when the array of objects has atype
as a prop, and also if the schema has a prop with the valueSchemaTypes.Array
. Using new Schema for subdocument fixes this but causes another issue with lean type for arrays, will create a new issue for thatSteps to Reproduce
Lemme know if I'm doing anything wrong with the schema. Or if it's a bug, I'll be happy to contribute, lemme me know how can I fix it.
Expected Behavior
It should give proper typings like before
_Originally posted by @imranbarbhuiya in #14095 (comment)
The text was updated successfully, but these errors were encountered: