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

[Types] properties like "caster" are not typed (SingleNestedPath) #10418

Closed
4 tasks
hasezoey opened this issue Jul 4, 2021 · 5 comments
Closed
4 tasks

[Types] properties like "caster" are not typed (SingleNestedPath) #10418

hasezoey opened this issue Jul 4, 2021 · 5 comments
Labels
discussion If you have any thoughts or comments on this issue, please share them! typescript Types or Types-test related issue / Pull Request

Comments

@hasezoey
Copy link
Collaborator

hasezoey commented Jul 4, 2021

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

What is the current behavior?
the options are un-typed

If the current behavior is a bug, please provide the steps to reproduce.

// NodeJS: 16.4.1
// MongoDB: 4.2-bionic (Docker)
import * as mongoose from "mongoose"; // [email protected]

const schema1 = new mongoose.Schema({ nestedPath1: { prop1: String } });
const schema2 = new mongoose.Schema({ nestedSchema: schema1 });
// @ts-ignore
console.log("schema1.nestPath1", schema1.path("nestedPath1"));
// @ts-ignore
console.log("schema2.nestedSchema", schema2.path("nestedSchema").caster);

// tslint-disable no-unused
schema2.eachPath((path, type) => {
  console.log("path,type", path, type);
  if (type instanceof mongoose.Schema.Types.Embedded) {
    // currently an error everywhere from typescript "property does not exist"
    type.caster;
    type.emit;
    type.on;
    type.path;
    type.base;
  }
});

What is the expected behavior?
the properties to be typed

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


options to do:

  • mongoose.Schema.Types.Embedded should extends EventEmitter
  • path should be typed
  • base should be typed
  • when possible '$isSingleNested' should also be typed
@IslandRhythms IslandRhythms added the typescript Types or Types-test related issue / Pull Request label Jul 6, 2021
@vkarpov15
Copy link
Collaborator

@hasezoey mongoose.Schema.Types.Embedded does not extend EventEmitter, it is a SchemaType. And path, base, and $isSingleNested are currently undocumented properties for internal use - why do you need to use them?

@hasezoey
Copy link
Collaborator Author

hasezoey commented Jul 7, 2021

mongoose.Schema.Types.Embedded does not extend EventEmitter

sorry, i just noticed it has many (if not even all) properties that an EventEmitter would have (like on, off,or *Listeners functions)

why do you need to use them?

  • as for path it would be nice to just have it in types, because it exists at runtime and from what i know is always the path name
  • as for base it could be directly used instead of having to supply it some other way (from what i know, it is what is supplied by schema options existingMongoose)
  • as for $isSingleNested, i have no clue for what its for, and just mentioned it, because it also exists and is accessible from the outside

i dont have an use-case for any of them at the moment, i just saw them that they are untyped, but still accessible from the outside and could be useful to others (at least path & base)

@vkarpov15
Copy link
Collaborator

You might be mixing up the subdocument schematype and the actual subdocument type. Subdocuments are event emitters, but subdocument schematypes are not.

@hasezoey
Copy link
Collaborator Author

that might be true, so what is schema.path() and schema.eachPath(the second parameter from the callback) supposed to be

and just to clarify, with subdocument-schematype you mean Schema.Types.* and with actual-subdocuments you mean Types.*? when yes, them i am mixing things up maybe, but if not, look at the reproduction code i provided, it clearly logs the output from schema.eachPath (both parameters) and it contained those values / functions on the output

@vkarpov15 vkarpov15 added this to the 5.13.4 milestone Jul 13, 2021
@vkarpov15
Copy link
Collaborator

schema.path() returns the subdocument schematype, e.g. instanceof mongoose.Schema.Types.Embedded.

And yes, the subdocument schematype does have those properties, but they're not meant to be user-facing. If there's a reason for them to be user facing I'm happy to add them to the TS bindings.

@vkarpov15 vkarpov15 removed this from the 5.13.4 milestone Jul 28, 2021
@vkarpov15 vkarpov15 added the discussion If you have any thoughts or comments on this issue, please share them! label Jul 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion If you have any thoughts or comments on this issue, please share them! typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

No branches or pull requests

3 participants