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

InferRawDocType returns unknown for every property instead of the right type #14658

Closed
1 task done
watch-janick opened this issue Jun 10, 2024 · 3 comments
Closed
1 task done
Labels
help wanted help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary Stale

Comments

@watch-janick
Copy link

Prerequisites

  • I have written a descriptive issue title

Mongoose version

8.4.x

Node.js version

20.x

MongoDB version

7.x

Operating system

macOS

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

12.7.x

Issue

Hello!

Following the documentation regarding InferRawDocType I'm facing a weird typing issue: every property of my schema is defined as unknown.

I'm using NestJS Mongoose Schema, could it be the problem?

My goal is to share the same type definition between my backend and frontend app.
I can't use the decorated class User on the frontend so I'm trying to get an inferred type that I can use on both.

Here's the code:

@Schema({ timestamps: true })
export class User {
  @Prop({ type: String, required: true })
  firstName: string

  @Prop({ required: true })
  lastName: string

  @Prop({ required: true })
  email: string

  @Prop({ enum: UserRoles, required: true })
  role: UserRoles

  @Prop({ type: [String], enum: Permissions, required: true })
  permissions: Permissions[]

  @Prop({ type: Date })
  lastLoginAt?: Date

  @Prop({ type: Boolean, default: true })
  isActive?: boolean
}

export type UserDocument = HydratedDocument<User>
export const UserSchema = SchemaFactory.createForClass(User)
export type UserType = InferRawDocType<User>

Here's what UserType gives:
Screenshot 2024-06-10 at 15 53 09

Any idea as to why the properties aren't typed correctly?

Thanks!

@watch-janick watch-janick added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels Jun 10, 2024
@vkarpov15
Copy link
Collaborator

InferRawDocType converts a Mongoose schema definition into a TypeScript type, so { firstName: { type: String, required: true } } to { firstName: string }. However, with NestJS, you're starting with the TypeScript type and deriving a schema from that, so I don't think you even need InferRawDocType.

We did some work to get InferRawDocType to handle User correctly, but it looks like the correct output for InferRawDocType<User> is just User anyway. Is there a reason why you don't just do export type UserType = User or something similar?

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 Jun 26, 2024
Copy link

github-actions bot commented Jul 1, 2024

This issue was closed because it has been inactive for 19 days and has been marked as stale.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary Stale
Projects
None yet
Development

No branches or pull requests

2 participants