Skip to content

Type inference for discriminator models are broken #15535

@lobabob

Description

@lobabob

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.16.2

Node.js version

22.13.1

MongoDB server version

8.0.4

Typescript version (if applicable)

5.7.3

Description

Discriminator models only have the _id and __v properties associated with their typings. All type inferences from its schema definition or the schema definition of its parent are lost.

Steps to Reproduce

const ParentSchema = new Schema({
  field1: {
    type: String,
    require: true,
  },
  field2: Number,
}, {
  discriminatorKey: 'field1',
})

const ParentModel = mongoose.model('Parent', ParentSchema)

const ChildSchema = new Schema({
  field3: String,
})

const ChildModel = ParentModel.discriminator('child', ChildSchema)

Typescript shows the fields associated with ChildModel and any documents created via ChildModel are just _id and __v when they should include field1, field2, and field3.

Expected Behavior

Correct type inference should show that fields associated with ChildModel and any documents created via ChildModel should include field1, field2, and field3.

Metadata

Metadata

Assignees

No one assigned

    Labels

    typescriptTypes or Types-test related issue / Pull Request

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions