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

Reusing array schema definitions #12611

Closed
1 task done
hoangbn opened this issue Oct 29, 2022 · 1 comment · Fixed by #12720
Closed
1 task done

Reusing array schema definitions #12611

hoangbn opened this issue Oct 29, 2022 · 1 comment · Fixed by #12720
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@hoangbn
Copy link

hoangbn commented Oct 29, 2022

Prerequisites

  • I have written a descriptive issue title

Mongoose version

6.7.0

Node.js version

16.18.0

MongoDB version

5.x

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

No response

Issue

I'm trying to migrate a project to use official typescript inferred schema definitions. One of the things I do is reuse common schema definitions. For example:

export const reusableFields = {
  description: { type: String, required: true },
  skills: { type: [Schema.Types.ObjectId], ref: "Skill", default: [] }
} as const;

export const firstSchema = new Schema({
  ...reusableFields,
  anotherField: String
})

export type Props = InferSchemaType<typeof firstSchema>;

Props typ is:

type Props = {
    description: string;
    skills: {
        1: unknown;
        0?: Types.ObjectId;
    };
    anotherField?: string;
}

Non-array types work as expected (description), but array types (skills) cannot be inferred. I haven't figured out how to make it work yet. Any suggestions?

@hoangbn hoangbn added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels Oct 29, 2022
@hasezoey hasezoey added the typescript Types or Types-test related issue / Pull Request label Oct 31, 2022
@vkarpov15 vkarpov15 added this to the 6.7.3 milestone Nov 7, 2022
@github-actions
Copy link

This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days

@github-actions github-actions bot added the Stale label Nov 22, 2022
@vkarpov15 vkarpov15 modified the milestones: 6.7.3, 6.7.4 Nov 22, 2022
@github-actions github-actions bot removed the Stale label Nov 23, 2022
@vkarpov15 vkarpov15 removed help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels Nov 23, 2022
vkarpov15 added a commit that referenced this issue Nov 25, 2022
fix(types): correctly infer ReadonlyArray types in schema definitions
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.

3 participants