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

Schema.path() always returns SchemaType and not the specific type of the supplied path #11987

Closed
2 tasks done
satyamtg opened this issue Jun 25, 2022 · 4 comments
Closed
2 tasks done
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@satyamtg
Copy link

satyamtg commented Jun 25, 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.0

Node.js version

16.8.0

MongoDB server version

5.x

Description

When I try to run Schema.path() on a mongoose Schema, I always get the result as SchemaType. However, in the types, I see many specific classes that extend SchemaType and have extra methods such as discriminator. I don't know if I'm doing something wrong, but I assume that TypeScript won't throw errors if correct SchemaType is sent as the resultant type of path()

Steps to Reproduce

Sample code to reproduce -

import { Schema, model, SchemaType, Document, Model, Types, SchemaTypes } from 'mongoose';

interface IUser {
	name: string;
	email: string;
	organization: Types.ObjectId;
 }
 
 const userSchema = new Schema<IUser>({
	name: { type: String, required: true },
	email: { type: String, required: true },
	organization: { type: Schema.Types.ObjectId, ref: 'Organization' }
  });

const pathResult = userSchema.path('name'); // I always get SchemaType as the result
@RAY-EZ
Copy link

RAY-EZ commented Jun 25, 2022

I also found this a bit confusing at first. But mongoose defines the path with the given Predefined Types eg String, Number, ObjectID.
mongoose.prototype.path here in example
they have passed second argument as Number. I also thought it should return Number while calling schema.path('path'). Calling schema.path('fullpath', Number) this creates a new path with name instance of SchemaNumber.

Anyway if you want get specific type out of SchemaType schema.path('parent.child').instance

@satyamtg
Copy link
Author

@RAY-EZ can you please elaborate a bit with a code example? I do know there is a getter and a setter for path. However, can you explain what you mean by fullpath and then parent.child. Also, I get this TS error while using .instance as you suggested -

Property 'instance' does not exist on type 'SchemaType'.ts(2339)

@RAY-EZ
Copy link

RAY-EZ commented Jun 25, 2022

@satyamtg I have tried every way to reproduce error. I think your typescript acting up restarting ts-server might help..

        ...
	name: { 
          firstName: {
               type: String, required: true 
               },
         lastName: {
         ...
  });

const pathResult = userSchema.path('name');

I modified your example a bit. suppose you have nested Path, like name has firstName. Now to access SchemaType
you would pass fullpath i.e name.firstName.

workaround to satisfy typescript
const pathResult = userSchema.path('name') as SchemaType<IUser>
I hope it works 🙂

@vkarpov15 vkarpov15 added this to the 6.4.2 milestone Jun 26, 2022
@vkarpov15 vkarpov15 added the typescript Types or Types-test related issue / Pull Request label Jun 26, 2022
@vkarpov15 vkarpov15 modified the milestones: 6.4.2, 6.4.3 Jul 1, 2022
@vkarpov15
Copy link
Collaborator

In 6.4.3 you'll be able to do userSchema.path<'name'>('name') to get back SchemaType<string>

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

3 participants