Skip to content
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

Closed
2 tasks done
imranbarbhuiya opened this issue Feb 21, 2024 · 4 comments · Fixed by #14430
Closed
2 tasks done

InferSchemaType returns unknown value #14367

imranbarbhuiya opened this issue Feb 21, 2024 · 4 comments · Fixed by #14430
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@imranbarbhuiya
Copy link

imranbarbhuiya commented Feb 21, 2024

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.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 from 8.0.2, after checking commits and reverting them, and found this pr is causing the issue. It happens when the array of objects has a type as a prop, and also if the schema has a prop with the value SchemaTypes.Array. Using new Schema for subdocument fixes this but causes another issue with lean type for arrays, will create a new issue for that

Steps to Reproduce

export const UserSchema = new Schema(
    {
        reminders: {
            type: [
                {
                    type: { type: SchemaTypes.String },
                    date: { type: SchemaTypes.Date },
                    toggle: { type: SchemaTypes.Boolean },
                    notified: { type: SchemaTypes.Boolean },
                },
            ],
            default: [
                { type: 'vote', date: new Date(), toggle: false, notified: false },
                { type: 'daily', date: new Date(), toggle: false, notified: false },
                { type: 'drop', date: new Date(), toggle: false, notified: false },
                { type: 'claim', date: new Date(), toggle: false, notified: false },
                { type: 'work', date: new Date(), toggle: false, notified: false },
            ],
        },
        avatar: {
            type: SchemaTypes.String,
        },
    },
    { timestamps: true },
);

export type IUser = InferSchemaType<typeof UserSchema>;

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)

@vkarpov15 vkarpov15 added this to the 8.2.1 milestone Feb 23, 2024
@vkarpov15 vkarpov15 added the typescript Types or Types-test related issue / Pull Request label Feb 23, 2024
@vkarpov15 vkarpov15 modified the milestones: 8.2.1, 8.2.2 Mar 4, 2024
@vkarpov15 vkarpov15 added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Mar 11, 2024
@vkarpov15
Copy link
Collaborator

You're right, this is due to the type: { type: SchemaTypes.String } property. Without type, everything compiles fine. We're investigating

@imranbarbhuiya
Copy link
Author

imranbarbhuiya commented Mar 16, 2024

You're right, this is due to the type: { type: SchemaTypes.String } property. Without type, everything compiles fine. We're investigating

the issue with nested type is fixed but now all the arrays are DocumentArray so I cant pass a normal array to this type, can't use .includes with normal types

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)

@vkarpov15 vkarpov15 reopened this Mar 20, 2024
@vkarpov15 vkarpov15 modified the milestones: 8.2.2, 8.2.4 Mar 20, 2024
@vkarpov15 vkarpov15 removed the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Mar 20, 2024
@vkarpov15 vkarpov15 reopened this Mar 28, 2024
@vkarpov15 vkarpov15 modified the milestones: 8.2.4, 8.2.5 Mar 28, 2024
@pavel-luhin
Copy link

Hi @imranbarbhuiya
Have you found any workaround? Mine is to pass object with array directly to create function. Maybe you found something else?

@vkarpov15
Copy link
Collaborator

We have a PR to fix a.roles.includes('a'); #14534.

For const a: IUser = { ... } that's unfortunately currently expected behavior. We'll bump up #13900 in priority, which will add a separate helper than can infer the raw document type, rather than inferSchemaType<>, which is meant to be more of an internal helper for Mongoose to figure out the hydrated document type.

@vkarpov15 vkarpov15 modified the milestones: 8.3.3, 8.4 Apr 26, 2024
vkarpov15 added a commit that referenced this issue Apr 29, 2024
types(schema): correctly infer Array<Schema.Types.*>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants