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

InterSchemaType does not properly infer "required" field for Schema.Types.ObjectId type #12205

Closed
2 tasks done
darwin403 opened this issue Aug 3, 2022 · 7 comments · Fixed by #12352
Closed
2 tasks done
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@darwin403
Copy link

darwin403 commented Aug 3, 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.5.0

Node.js version

16.13.1

MongoDB server version

6.x

Description

A schema field with {type: Schema.Types.ObjectId, required: true} cause faulty inference of Schema type. But, {type: Schema.Types.ObjectId, required: false} , {type: String, required: true} seem to work

Steps to Reproduce

The following works:

const campaignSchema = new Schema(
  {
    client: {
      type: Schema.Types.ObjectId,
      trim: true,
      ref: "User",
      required: false,
    },
  },
  { timestamps: true }
);

type ICampaign = InterSchemaType<ICampaignSchema>; // {client?: Types.ObjectId | undefined }

The following doesn't work:

const campaignSchema = new Schema(
  {
    client: {
      type: Schema.Types.ObjectId, // works when the type is changed to "String"
      trim: true,
      ref: "User",
      required: true, // fails to interpret schema
    },
  },
  { timestamps: true }
);

type ICampaign = InterSchemaType<ICampaignSchema>; // DOES NOT return {client:  Types.ObjectId }

Screenshot-from-2022-08-03-09-31-43

Expected Behavior

type ICampaign = InterSchemaType<ICampaignSchema>;  // {client:  Types.ObjectId }
@vkarpov15 vkarpov15 added this to the 6.5.4 milestone Aug 8, 2022
@IslandRhythms IslandRhythms added the needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue label Aug 8, 2022
@IslandRhythms
Copy link
Collaborator

What is inferModelType suppose to be?

@darwin403
Copy link
Author

@IslandRhythms Sorry it was a typo. I meant InterSchemaType, just updated the issue. The inferModelType is just my custom type and can be ignored. The problem I'm highlighting only concerns InterSchemaType

@IslandRhythms IslandRhythms added needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Aug 24, 2022
@IslandRhythms
Copy link
Collaborator

Cannot find name 'InterSchemaType'

@Uzlopak
Copy link
Collaborator

Uzlopak commented Aug 24, 2022

@mohammad0-0ahmad

@mohammad0-0ahmad
Copy link
Contributor

mohammad0-0ahmad commented Aug 24, 2022

@darwin403 has meant the following:

const campaignSchema = new Schema(
  {
    client: {
      type: Schema.Types.ObjectId,
      trim: true,
      ref: 'User',
      required: true
    }
  },
  { timestamps: true }
);

type ICampaign = InferSchemaType<typeof campaignSchema>;

@mohammad0-0ahmad
Copy link
Contributor

Yes, it returns a wrong result.
It related to #12168, I think.
@vkarpov15 would you like to reopen it, so @iammola can fix it, or what do you think ?

vkarpov15 added a commit that referenced this issue Aug 29, 2022
@vkarpov15 vkarpov15 modified the milestones: 6.5.4, 6.6 Aug 29, 2022
@vkarpov15 vkarpov15 added typescript Types or Types-test related issue / Pull Request and removed needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity labels Aug 29, 2022
@vkarpov15 vkarpov15 modified the milestones: 6.6, 6.7 Sep 8, 2022
@ryaposov
Copy link

I can see the issue is in progress already, but just to confirm I have a similar issue even without specifying required.

...
type: {
    type: String,
    enum: ['income', 'expence', 'write-off'],
    required: true
  },
  procurementOrderId: {
    type: Schema.Types.ObjectId,
    ref: 'procurementOrders',
  },

Produces the following (notice type field resulting in unknown):

image

Removing procurementOrderId field from schema makes types work again perfectly.

vkarpov15 added a commit that referenced this issue Oct 2, 2022
feat(types+schema): allow defining schema paths using mongoose.Types.* to work around TS type inference issues
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
6 participants