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

TypeScript Bug: error when defining required array #10674

Closed
rattrayalex opened this issue Sep 3, 2021 · 2 comments
Closed

TypeScript Bug: error when defining required array #10674

rattrayalex opened this issue Sep 3, 2021 · 2 comments
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@rattrayalex
Copy link

Do you want to request a feature or report a bug?

bug

What is the current behavior?

TypeScript Playground Link

import mongoose, { Model, Schema } from "mongoose"; // v5.13.8
export type Foo = {
  bar: string
  schedule: {
    date: string;
    hours: number;
  }[];
};

type FooModel = Model<Foo>;

const FooSchema = new Schema<Foo, FooModel, Foo>(
  {
    bar: { type: String },
    schedule: {
      type: [
        {
          date: { type: String, required: true },
          hours: { type: Number, required: true },
        },
      ],
      required: true,
    },
  }
)

Error:

        Type '{ date: { type: StringConstructor; required: true; }; hours: { type: NumberConstructor; required: true; }; }' is not assignable to type 'Schema<{ date: string; hours: number; }, Model<{ date: string; hours: number; }, any, any>, {}>'.
          Object literal may only specify known properties, and 'date' does not exist in type 'Schema<{ date: string; hours: number; }, Model<{ date: string; hours: number; }, any, any>, {}>'.

(easier to see on the TS playground link, above).

If I change my schema to this, it works fine:

    schedule: [
        {
          date: { type: String, required: true },
          hours: { type: Number, required: true },
        },
      ],

but of course, I want the schedule to be required!

What is the expected behavior?

No type error

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

{
"typescript": "^4.3.5",
"mongodb": "^3.5.7",
"mongoose": "5.13.8",
"@types/mongodb": "3.6.20"
}
@vkarpov15 vkarpov15 added this to the 6.0.6 milestone Sep 6, 2021
@vkarpov15 vkarpov15 added the typescript Types or Types-test related issue / Pull Request label Sep 6, 2021
@vkarpov15 vkarpov15 modified the milestones: 6.0.6, 6.0.7 Sep 6, 2021
@vkarpov15
Copy link
Collaborator

I can confirm this was fixed in v6.0.4 with #10605. We'll move this to our v5.13.10 milestone so we know to backport it when we have time, but I don't expect us to get to that for another week or two. Are you able to upgrade to Mongoose 6.x or is there something blocking you?

@vkarpov15 vkarpov15 modified the milestones: 6.0.7, 5.13.10 Sep 18, 2021
@rattrayalex
Copy link
Author

Awesome thank you!

No known blockers, just general fear of breaking changes and upgrades (and frankly while the migration guide looks comprehensive, it's a little hard to quickly glean what issues we should mostly look out for). But we can definitely give it a try!

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

No branches or pull requests

2 participants