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 does not infer subdocuments/nested paths #12011

Closed
2 tasks done
heilmela opened this issue Jun 29, 2022 · 3 comments · Fixed by #12007
Closed
2 tasks done

InferSchemaType does not infer subdocuments/nested paths #12011

heilmela opened this issue Jun 29, 2022 · 3 comments · Fixed by #12007
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@heilmela
Copy link

heilmela commented Jun 29, 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

16.13.0

MongoDB server version

5.0

Description

Basically, I took the example from the docs and pluralized property name. The reslulting schema type is inferred as { ..., names?: unknown[] | undefined }.

The same thing happens when using a nested document, names: new Schema({}) and when using names: {type: [{...}]} the only array type which seems to be inferred correctly are primitive types e.g. names: [String] is inferred correctly as string[]

Steps to Reproduce

const schema = new mongoose.Schema({
  names: [{ type: String, required: true }],
  email: { type: String, required: true },
  avatar: String,
});

type User = InferSchemaType<typeof schema>;
// InferSchemaType will determine the type as follows:
// type User = {
//   email: string;
//   avatar?: string | undefined;
//   names?: unknown[] | undefined;
// };

Expected Behavior

const schema = new mongoose.Schema({
  names: [{ type: String, required: true }],
  email: { type: String, required: true },
  avatar: String,
});

type User = InferSchemaType<typeof schema>;
// InferSchemaType will determine the type as follows:
// type User = {
//   email: string;
//   avatar?: string | undefined;
//   name: string[] | undefined;
// };
@heilmela
Copy link
Author

heilmela commented Jun 29, 2022

You might be correct, the example was a bit rash. However the problem still remains when using any non primitive array e.g.

const schema = new mongoose.Schema({
  names: {
    type: [
      {
        first: String,
        last: String,
      },
    ],
    required: true,
  },
  email: { type: String, required: true },
  avatar: String,
});

@iammola
Copy link
Contributor

iammola commented Jun 29, 2022

Yeah, you're right @heilmela ... I'm about to push a commit to fix that in the PR I've linked above. It should allow defining schema paths like this as well.

names: [{ type: String, required: true }],

The only issue is, that it'll match the string type but not make it required.

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

@heilmela Thanks for reporting this issue.

@vkarpov15 vkarpov15 added this to the 6.4.2 milestone Jul 1, 2022
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.

5 participants