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 generates Schema.Types.ObjectId instead of the Types.ObjectId we want #12002

Closed
2 tasks done
keysking opened this issue Jun 28, 2022 · 6 comments · Fixed by #12007
Closed
2 tasks done
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@keysking
Copy link

keysking commented Jun 28, 2022

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

6.4.1

Node.js version

18.*

MongoDB server version

5.x

Description

I learned from the documentation that when a Schema has a field whose type is Schema.Types.ObjectId, the corresponding Typescript type should be Types.ObjectId.
However, when I use InferSchemaType to generate the type, the type it generated for me is Schema.Types.ObjectId.
This prevents me from using the new Types.ObjectId () to pass into my field.

Steps to Reproduce

import { InferSchemaType, model, Schema } from 'mongoose';
const productSchema = new Schema({
  typeId: { type: Schema.Types.ObjectId, ref: 'ProductType' },
});
export type Product = InferSchemaType<typeof productSchema>;
export const Product = model<Product>('Product', productSchema);

const add(product: Product) {}

add({ typeId: new Types.ObjectId()});

image

Expected Behavior

No response

@IslandRhythms IslandRhythms added the typescript Types or Types-test related issue / Pull Request label Jun 29, 2022
@mohammad0-0ahmad
Copy link
Contributor

@keysking Thanks for reporting this issue.

@keysking
Copy link
Author

@keysking Thanks for reporting this issue.

My pleasure😁.

@vkarpov15 vkarpov15 added this to the 6.4.2 milestone Jul 1, 2022
@borispinus
Copy link

apparently encountered with similar problem but in my case the field is array of refs:

const schema = new mongoose.Schema({
  goods: [{ type: mongoose.Schema.Types.ObjectId, ref: "Goods" }],
});

type Promo = InferSchemaType<typeof schema>

the inferred type is goods: { type?: mongoose.Types.ObjectId | undefined; ref?: unknown; }[] istead of expected goods: mongoose.Types.ObjectId[]

@jbolter
Copy link

jbolter commented Sep 30, 2022

apparently encountered with similar problem but in my case the field is array of refs:

const schema = new mongoose.Schema({
  goods: [{ type: mongoose.Schema.Types.ObjectId, ref: "Goods" }],
});

type Promo = InferSchemaType<typeof schema>

the inferred type is goods: { type?: mongoose.Types.ObjectId | undefined; ref?: unknown; }[] istead of expected goods: mongoose.Types.ObjectId[]

Any fix that you found? I'm running into the same issue.

@borispinus
Copy link

Nope, I just used type casting to force mongoose.Types.ObjectId[]

@jbolter
Copy link

jbolter commented Oct 10, 2022

I ended up just creating the schema manually instead of using InferSchemaType to avoid the bug.

@Automattic Automattic locked and limited conversation to collaborators Oct 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants