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: array of subdocuments with a "type" key is not correctly inferred #12882

Closed
2 tasks done
JavaScriptBach opened this issue Jan 6, 2023 · 0 comments · Fixed by #12884
Closed
2 tasks done
Labels
typescript Types or Types-test related issue / Pull Request

Comments

@JavaScriptBach
Copy link
Contributor

JavaScriptBach commented Jan 6, 2023

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

Node.js version

16.17.1

MongoDB server version

4.4

Typescript version (if applicable)

No response

Description

If I have an array of objects containing a key named "type": InferSchemaType does not correctly infer it.

Steps to Reproduce

const schema = new Schema({
  fooArray: {
    type: [{
      type: {
        type: String,
        required: true,
      },
      foo: {
        type: Number,
        required: true,
      }
    }],
    required: true,
  }
})
type schemaT = InferSchemaType<typeof schema>;

Expected Behavior

I should get the following type inferred:

type schemaT = {
    fooArray: {
        type: string;
        foo: number;
    }[];
}

Instead I get:

type schemaT = {
    fooArray: {
        type?: string | undefined;
        required?: unknown;
    }[];
}
@IslandRhythms IslandRhythms added the typescript Types or Types-test related issue / Pull Request label Jan 9, 2023
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.

2 participants