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

Missing inferred enum property when type is Number #12242

Closed
2 tasks done
Catatomik opened this issue Aug 9, 2022 · 1 comment · Fixed by #12463
Closed
2 tasks done

Missing inferred enum property when type is Number #12242

Catatomik opened this issue Aug 9, 2022 · 1 comment · Fixed by #12463
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@Catatomik
Copy link

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

Node.js version

17.3.0

MongoDB server version

5.0.5

Description

When you precise an enum property on a Schema, and when type is Number, the type doesn't take in account the enum array, but InferSchemaType resolves to number.

Steps to Reproduce

const dbExample = new Schema(
  {
    active: { type: Number, enum: [0, 1], required: true }
  }
);

type Example = InferSchemaType<typeof dbExample>;

Here, in type Example, active will be number.

Expected Behavior

Following the previous example, active should be 0 | 1.

@AbdelrahmanHafez AbdelrahmanHafez added the typescript Types or Types-test related issue / Pull Request label Aug 15, 2022
@hasezoey
Copy link
Collaborator

i dont know if this is even possible with current typescript, without manually doing [] as const (where [] is the enum array), see This Typescript Stackoverflow Question

@vkarpov15 vkarpov15 added this to the 6.5.7 milestone Sep 14, 2022
vkarpov15 added a commit that referenced this issue Sep 22, 2022
fix(types): infer number enum types from schema if using `enum: [0, 1] as const`
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.

4 participants